C++ Practice Problems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

C++ Practice programs 1

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;
}

3. Write a program to calculate area of circle.


#include<iostream>
using namespace std;
int main()
{
float r,area;
cout<< "\nEnter radius of circle in meter : ";
cin>>r;
area = 3.14*r*r;
cout<<"Area of circle in m^2 is : "<<area;
return 0;
}
4. Any integer is input by the user. Write a program to find out whether it is an

odd number or even number.


#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number : ";
cin>>a;
if(a%2==0)
cout<<"The number is even";
else
cout<<"The number is odd";
return 0;
}
5. Write a program to print number from 1 to 10
#include<iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<"\n";
i++;
}
return 0;
}

6. Find the absolute value of a number entered by the user.


#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter any number:";
cin>>a;
if(a>0)
cout<<"The absolute value of number is:"<<a;
else
cout<<"The absolute value of number is:"<<-(a);
return 0;
}
7. If the ages of Abebe, Yared and Kebede are input by the user, write a

program to determine the youngest of the three.


#include<iostream>
using namespace std;
int main()
{
int abebe_age,yared_age,kebede_age;
cout<<"Enter Abebe's age:";
cin>>abebe_age;
cout<<"Enter Yared's age:";
cin>>yared_age;
cout<<"Enter kebede's age:";
cin>>kebede_age;
if (abebe_age<yared_age && abebe_age<kebede_age)
cout<<"Abebe is the youngest";
else if(kebede_age<abebe_age && kebede_age<yared_age)
cout<<"Kebede is the youngest";
else
cout<<"Yared is the youngest";
return 0;
}

8. Write a program to find the roots of a quadratic equation of type ax2+bx+c

where a is not equal to zero.


//a program to find the roots of a quadratic equation of type
//ax2+bx+c where a is not equal to zero.
//Programmed by:
//Date:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}
cout<<"\nRoot 1= "<<root1;
cout<<"\nRoot 2= "<<root2;
return 0;
}

9. The marks obtained by a student in 5 different subjects are input by the user.

The student gets a division as per the following rules:


Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student
#include<iostream>
using namespace std;
int main()
{
int sub1,sub2,sub3,sub4,sub5,average;
cout<<"Enter marks of five subjects : ";
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
average=(sub1+sub2+sub3+sub4+sub5)/5;
if(average>=60)
cout<<"Ist division";
else if(average>=50)
cout<<"IInd division";
else if(average>=40)
cout<<"IIIrd division";
else cout<<"Fail" ;
return 0;
}
10. Write a program to calculate the sum of first 10 natural number.
#include<iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(i<=10)
{
sum+=i; i++;
}
cout<<"Sum ="<<sum;
return 0;
}

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

power p and display the result.


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int p;
double n,r;
cout<<"Enter a Number : ";
cin>>n;
cout<<"Enter an Exponent : ";
cin>>p;
r=pow(n,p);
cout<<"\nThe result of " <<n <<" exponent " <<p<<" is "<<r;
return 0;

The for loop


15.

Write a program to print following shapes


(i)

(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)

using namespace std;

#include<iostream>

int main()

using namespace std;

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

the expression result with the selected operator on the operands.


#include<iostream>
using namespace std;
int main()
{
int num1,num2;
char op;
cout<<"Enter two numbers:"<<endl;
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Enter the operator (+,-,*,/) :";
cin>>op;
switch (op)
{
case '+':
cout<<"\n\tThe sum is:"<<num1+num2;
break;
case '-':
cout<<"\n\t The difference is:"<<num1-num2;
break;
case '*':
cout<<"\n\t The product is:"<<num1*num2;
break;
case '/':
{
cout<<"\n\t The quotient is:"<<num1/num2;
cout<<"\n\t The remainder is:"<<num1%num2;
break;
}
default:
cout<<"\n\t Invalid Operator!! Please Enter a valid operator
again!";
}
return 0;
}
10

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.

(using for loop)


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int num;double sq_root;
for(num=1;num<=99;num++)
{
sq_root=sqrt(num);
cout<<"The square root of "<<num<< " is "<<sq_root;
cout<<endl;
}
return 0;
}
11

19. Write a program that that calculates square roots of numbers 1 to 99.

(Using the while loop)


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int num=1;
double sq_root;
while(num<=99)
{
sq_root=sqrt(num);
cout<<"The square root of "<<num<< " is "<<sq_root;
cout<<endl;
num++;
}
return 0;
}
20. A program that prints even numbers from 0 to 100
#include<iostream>
using namespace std;
int main()
{
int num;
for(num=0;num<=100;num++)
{
if(num%2==0)
cout<<num<<'\t'; // \t is used to insert tab b/n output numbers
}
return 0;
}
12

21. Write a C++ program that accepts mark out of 100 and calculates Grade based

on the following criteria


Mark out of 100
>= 80
>=60
>=50
>=40
<40

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

order from smallest to largest.


#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Please press Enter after inserting a number.\n";
cout<<"Enter three numbers:\n";
cin>>a>>b>>c;
if((a>b)&&(b>c))
cout<<"The numbers you entered in ascending order is: "
<<c<<","<<b<<","<<a;
else if((a>c)&&(c>b))
cout<<"The numbers you entered in ascending order is: "
<<b<<","<<c<<","<<a;
else if((b>c)&&(c>a))
cout<<"The numbers you entered in ascending order is: "
<<a<<","<<c<<","<<b;
else if((b>a)&&(a>c))
cout<<"The numbers you entered in ascending order is: "
<<c<<","<<a<<","<<b;
else if((c>a)&&(a>b))
cout<<"The numbers you entered in ascending order is: "
<<b<<","<<a<<","<<c;
else
cout<<"The numbers you entered in ascending order is: "
<<a<<","<<b<<","<<c;
return 0;
}
14

23. Write a C++ program that adds the even numbers between 0 and any positive

integer number given by the user.


//
//
//
//
//
//

A program that adds the even numbers between 0 and any


positive integer number given by the user.
Programmed by: Your Name
ID. No: Your ID Number
Section:
Date: Date of Program created

#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

of them are larger than 50, and display this result


// A program that reads 10 integers from the keyboard
// and count how many of them are larger than 50
// Programmed by: Your Name
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e,f,g,h,i,j;
cout<<"Enter ten numbers in the range from 0 to 100: ";
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
cout<<endl;
int count=0;
if(a>50)
count++;
if(b>50)
count++;
if(c>50)
count++;
if(d>50)
count++;
if(e>50)
count++;
if(f>50)
count++;
if(g>50)
count++;
if(h>50)
count++;
if(i>50)
count++;
if(j>50)
count++;
cout<<"The number of numbers greater than 50 is: "<<count;
return 0;
}

16

25. Write a program that reads 10 integers from the keyboard and count how many

of them are larger than 50, and display this result


(Using Array- will be discussed in chapter 5)
// A program that reads 10 integers from the keyboard
// and count how many of them are larger than 50
// Programmed by: Your Name
#include<iostream>
using namespace std;
int main()
{
int num[10]; //
cout<<"Enter 10 numbers: ";
cout<<endl;
cin>>num[0];
int count=0;
if(num[0]>50)
count++;
for (int i = 1; i < 10; i++)
{
cin>>num[i];
if (num[i] > 50)
count++;
}
cout<<"Count= "<<count;
return 0;
}

17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy