Important Coding Questions 2 CS101
Important Coding Questions 2 CS101
CLASS: CS-SECTION H.
REG NO: 2023428
bool prime(int num) //intiallizong the function that will determine if the no is
prime or not.
{
if(num<=1)
{
return false;
}
// the for loop ahead would start from two and would only check the first 2
numbers between I and the user input number as it would be sufficient to see if
the remainder is zero to make the user input non-prime number.
for (int i=2; i<=num/2; i++)
{
if (num%i==0)
{
return false;
}
}
return true;
}
int main()
{
int num;
cout<<"ENTER A NUMBER"<<endl;
cin>>num;
if(prime(num))
{
cout<<num<<" is a prime number."<<endl;
}
else
{
cout<<num<<" is not a prime number."<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
CODE:
#include <iostream>
using namespace std;
int main()
{
int hours, minutes;
cout << "ENTER THE NUMBER OF HOURS: " << endl;
cin >> hours;
displayTime(hours, minutes);
return 0;
}
SOLUTION:
CODE:
#include <iostream>
#include <cmath>
using namespace std;
double distance(double x1, double x2, double y1, double y2) //initializing the
funct to make the formula code of the finding the distance between 2 points.
{
double answer= sqrt(pow(x2-x1, 2)+ pow(y2-y1, 2));
return answer;
}
int main()
{
double x1, x2, y2, y1;
cout<<"ENTER THE COORDINATES OF FIRST POINT I.E (X1, Y1)"<<endl;
cin>>x1>>y1;
cout<<"ENTER THE COORDINATES OF SECOND POINT I.E (X2, Y2)"<<endl;
cin>>x2>>y2;
cout<<" THE FIRST POINT IS "<<"("<<x1<<","<<y1<<")"<<endl;
cout<<" THE SECOND POINT IS "<<"("<<x2<<","<<y2<<")"<<endl;
cout<<"THE DISTANCE BETWEEN THE TWO POINTS IS "<<distance(x1,x2,y1,y2);
cout<<endl;
return 0;
}
SOLUTION:
If the player's guess is incorrect, your program should loop until the player finally gets
the number right. Your program should keep telling the player Too high or Too low to
help the player “zero in” on the correct answer.
CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0)); // for setting a unique random number everytime the program
runs.
char playagain;
do { //using do while loop, the do will first always enter in another do and
ask for the guesses until the correct guess is reached after which it will ask
the user if it want’s to repeat the action of the code I.e the game.
int a = 1 + rand() % 100;
int guess;
do
{
cout<<"Please type your guess:";
cin>>guess;
if(guess == a) {
cout<<"Excellent! You guessed the number."<<endl;
} else if (guess < a)
{
cout<<"Too low. Try again."<<endl;
} else {
cout<<"Too high. Try again."<<endl;
}
}while(guess != a);
} while(playagain=='y' || playagain=='Y');
SOLUTION:
int fib(int n)
{
int a=0;
int b=1;
int nexterm=0;
for(int i=2; i<=n; i++) //using loop to find the nextterm and starting it from 2
as the the first 2 terms are already known, also renewing the terms so that if
the user asks for an nth term, the program can sum the last 2 known numbers to
give the nth term.
{
nexterm=a+b;
a=b;
b=nexterm;
}
if(n==0)
{return 0;}
else
{return b;}
}
int main()
{
int n;
cout<<"ENTER THE VALUE OF THE N FOR THE NTH FIBONACCI NUMBER"<<endl;
cin>>n;
if(n<0)
{
cout<<"INVALID INPUT. ENTER A NON-NEGATIVE INTEGER"<<endl;
}
cout<<"THE "<<n<<" TERM OF THE FIBONACCI SERIES IS "<<fib(n)<<endl;
}
SOLUTION(PART 1):
double fib(double n)
{
if (n==0)
{
double a=0.0;
return a;
}
if (n==1)
{
// for the given Fibonacci series the first 2 values are give and if the user
asks for them they will be simply outputted.
double b=1.0;
return b;
}
// how ever if the user asks for any other term, the recursive funct will run to
first achieve the 2 consecutive number just before that specific term and
eventually sum them to give the output.
return fib(n-2)+ fib(n-1);
}
int main()
{
double n;
cout<<"ENTER THE VALUE OF THE N FOR THE NTH FIBONACCI NUMBER"<<endl;
cin>>n;
if(n<0)
{
cout<<"INVALID INPUT. ENTER A NON-NEGATIVE double"<<endl;
}
else
{
cout<<"THE "<<n<<" TERM OF THE FIBONACCI SERIES IS "<<fib(n)<<endl;
return 0.0;
}
}
SOLUTION(PART2):
(same number even the digits are reversed). Explain the base case and recursive case
for this function.)
int sumofdigits(int n)
{
//doing the task of finding the sum of the number for which the digits are chosen
from the number given, we output the user input number if it is less than 10 as
there would be nothing to sum up with.
if(n<10)
{
return n;
}
// that was above the base case and thus if the 2 digit or more digit no. is give
the program runs by taking the last digit of that number first and recursively
calling itself reducing the number by one digit, i.e again taking its last digit
and adding with the previous. This practice will eventually run until the base
case is reached.
else
{
return n%10+sumofdigits(n/10);
}
}
int main()
{
int n;
cout<<"ENTER THE DIGITS CONSECUTIVETLY, WHOSE SUM IS REQUIRED"<<endl;
cin>>n;
cout<<"THE REQUIRED SUM IS "<<sumofdigits(n)<<endl;
return 0;
}
SOLUTION(PART 1):
int main()
{
int number;
cout << "ENTER A NUMBER TO CHECK IF IT IS PALINDROME" << endl;
cin >> number;
int reversenumber = 0;
SOLUTION(PART 2):
We have set the base case equal to zero as it checkes that all the digits
have been processed in the original number, when it is true the funct will
output the original number which is now the reversed number.
The recursive function is defined by the else block. In this case the
function is not in the base case meaning the number is not zero. It will
extract the last digit of the number and update the reverse number, it
also updates the number in use by dividing it by 10 and removing the lst
digit as it is already moved as first digit in the reversed number.
The function would recursively call itself until the base case is satisfied.