C++ Practice Problems
C++ Practice Problems
C++ Practice Problems
1. A program which accepts two numbers and displays the greater one
#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number:";
cin>>num2;
cout<<"\nAnnounce the maximum:\n";
if (num1>num2)
cout<<"The Greater is :"<<num1;
else if (num2>num1)
cout<<"The Greater is :"<<num2;
else
cout<<"The two numbers are equal.";
return 0;
}
2. Write a program which accept temperature in Fahrenheit and print it in
centigrade
#include<iostream>
using namespace std;
int main()
{
double F,C;
cout<< "Enter temperature in Fahrenheit : ";
cin>>F;
C=5*(F-32)/9;
cout<<"Temperature in Celsius is : "<<C;
return 0;
}
9. The marks obtained by a student in 5 different subjects are input by the user.
11. Write a program to find the factorial value of any number entered through the
keyboard.
#include<iostream>
using namespace std;
int main()
{
int n,fact=1;
cout<<"Enter any number : ";
cin>>n;
while(n>=1)
{
fact*=n; n--;
}
cout<<"Factorial :"<<fact;
return 0;
}
12. Write a program that asks the user to enter an hour value and a minute value.
The main( ) function should then pass these two values to a type int function
that displays the two values in the format shown in the following sample run:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include<iostream>
using namespace std;
int main()
{
int hours,minutes;
cout<<"Enter the number of hours: ";
cin>>hours;
cout<<"Enter the number of minutes: ";
cin>>minutes;
cout<<"Time: "<<hours<<":"<<minutes;
return 0;
}
13. What is the output of the following C++ program if the user enters number
5?
#include<iostream>
using namespace std;
int main()
{
int a;
int b=4;
cout<<"Enter a number: ";
cin>>a;
cout<<"The number you entered is: "<<a;
for(int i=4;i<=a;i++)
{
b=b+i;
}
cout<<"\nThe value of b is: "<<b;
}
14. Write a simple C++ program that accepts a number from a user raise it a
(i)
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}
return 0;;
}
(ii)
(ii)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
cout<<" ";
for(k=1;k<=2*i-1;k++)
cout<<"*";
cout<<endl;
}
return 0;
}
(iii)
(iv)
(iii)
#include<iostream>
(iv)
#include<iostream>
int main()
int main()
int i,j,k;
for(i=1;i<=5;i++)
int i,j,k;
for(i=1;i<=5;i++)
for(j=1;j<i;j++)
cout<<" ";
for(j=1;j<=5-i;j++)
for(k=9;k>=2*i-1;k--)
cout<<" ";
cout<<"*";
for(k=1;k<=2*i-1;k++)
cout<<endl;
cout<<i;
cout<<endl;
return 0;
return 0;;
}
16. This program prompts user to enter an operator and 2 operands and generate
17. Write a program that accepts an integer from the user and checks if it is divisible
by 2, 3 or 5
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter a number: ";
cin>>a;
if(a%2==0)
cout<<a<<" is divisible by 2.";
else if(a%3==0)
cout<<a<<" is divisible by 3.";
else if(a%5==0)
cout<<a<<" is divisible by 5.";
else
cout<<a<< " is not divisible by 2,3, and 5.";
return 0;
}
18. Write a program that that calculates square roots of numbers 1 to 99.
19. Write a program that that calculates square roots of numbers 1 to 99.
21. Write a C++ program that accepts mark out of 100 and calculates Grade based
Grade
A
B
C
D
F
#include<iostream>
using namespace std;
int main()
{
double mark;
cout<<"Enter your marks out of 100: ";
cin>>mark;
if(mark>=80)
cout<<"Your Grade
else if(mark>=60)
cout<<"Your Grade
else if(mark>=50)
cout<<"Your Grade
else if(mark>=40)
cout<<"Your Grade
else
cout<<"Your Grade
is A";
is B";
is C";
is D";
is F";
return 0;
}
13
22. Write a program that receives 3 integer numbers and display them in ascending
23. Write a C++ program that adds the even numbers between 0 and any positive
#include<iostream>
using namespace std;
int main()
{
int num,sum=0;
cout<<"Enter a number: ";
cin>>num;
for(int i=0;i<num;i++)
if(i%2==0)
sum=sum+i;
cout<<"The sum of even numbers between 0 and "<<num<<" is:"
<<sum;
return 0;
}
15
24. Write a program that reads 10 integers from the keyboard and count how many
16
25. Write a program that reads 10 integers from the keyboard and count how many
17