C++ Practice Problems
description
Transcript of C++ Practice Problems
-
1
C++ Practice programs 1
1. A program which accepts two numbers and displays the greater one
#include
using namespace std;
int main()
{
int num1,num2;
coutnum1;
coutnum2;
cout
-
2
3. Write a program to calculate area of circle. #include
using namespace std;
int main()
{
float r,area;
cout>r;
area = 3.14*r*r;
cout
-
3
6. Find the absolute value of a number entered by the user.
#include
using namespace std;
int main()
{
int a;
couta;
if(a>0)
cout
-
4
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
#include
using namespace std;
int main()
{
float a,b,c,d,root1,root2;
couta>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout
-
5
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
using namespace std;
int main()
{
int sub1,sub2,sub3,sub4,sub5,average;
coutsub1>>sub2>>sub3>>sub4>>sub5;
average=(sub1+sub2+sub3+sub4+sub5)/5;
if(average>=60)
cout
-
6
11. Write a program to find the factorial value of any number entered through the
keyboard.
#include
using namespace std;
int main()
{
int n,fact=1;
coutn;
while(n>=1)
{
fact*=n; n--;
}
cout
-
7
13. What is the output of the following C++ program if the user enters number
5?
#include
using namespace std;
int main()
{
int a;
int b=4;
couta;
cout
-
8
The for loop
15. Write a program to print following shapes (i) (ii)
(i) (ii) #include
using namespace std;
int main()
{
int i,j;
for(i=1;i
-
9
(iii) (iv)
(iii)
#include
using namespace std;
int main()
{
int i,j,k;
for(i=1;i
-
10
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
using namespace std;
int main()
{
int num1,num2;
char op;
cout
-
11
17. Write a program that accepts an integer from the user and checks if it is divisible
by 2, 3 or 5
#include
using namespace std;
int main()
{
int a;
couta;
if(a%2==0)
cout
-
12
19. Write a program that that calculates square roots of numbers 1 to 99.
(Using the while loop)
#include
#include
using namespace std;
int main()
{
int num=1;
double sq_root;
while(num
-
13
21. Write a C++ program that accepts mark out of 100 and calculates Grade based
on the following criteria
Mark out of 100 Grade
>= 80 A
>=60 B
>=50 C
>=40 D
=80)
cout
-
14
22. Write a program that receives 3 integer numbers and display them in ascending
order from smallest to largest.
#include
using namespace std;
int main()
{
int a,b,c;
coutb>>c;
if((a>b)&&(b>c))
cout
-
15
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
using namespace std;
int main()
{
int num,sum=0;
coutnum;
for(int i=0;i
-
16
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
using namespace std;
int main()
{
int a,b,c,d,e,f,g,h,i,j;
couta>>b>>c>>d>>e>>f>>g>>h>>i>>j;
cout50)
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
-
17
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
using namespace std;
int main()
{
int num[10]; //
cout>num[i];
if (num[i] > 50)
count++;
}
cout