CPP Exercises
CPP Exercises
CPP Exercises
6 to
2: Write a C++ program to prompt the user to input her/his name and print this name on the
screen
3. Write a C++ program that prompts the user to input three integer values and find the greatest
Example:
4. Write a C++ program to compute the real roots of the equation: ax2+bx+c=0.
The program will prompt the user to input the values of a, b, and c. It then computes the real
x1=-b+(b2-4ac)1/2/2a
x=-b-(b2-4ac)1/2/2a
5. Write a C++ program which accepts temperature in Farenheit and converts it to centigrade.
6. Write a program to swap the values of two variables (You can use a function)
7. Write a program to check whether the given number (accepted from the keyboard) is even or
odd.
3. int x = 10, y;
y = x++;
cout << y;
4. int x = 10, y;
y = x++;
cout << x;
5. int x = 10;
y = x + x++;
cout << y;
7. int x = 10,y;
y = ++x + x++ + x;
cout << y;
8. int x = 10, y;
y = x++ + x + ++x;
cout << y;
cout<<fixed<<setprecision(2)<<net;
9. Write a C++ programs that produce the following patterns.
1. *
**
***
****
*****
2. *
**
***
****
*****
3. 1
222
33333
4444444
555555555
//Solution of (i)
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}
return 0;
}
//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}
return 0;
}
10. Write a C++ program using function which accepts two integers as an argument and return its
sum. Call this function from main( ) and print the results in main( )
11. Raising a number to a power p is the same as multiplying n by itself p times. Write a function
called power that takes two arguments, a double value for n and an int value for p, and return the
result as double value.
a. #include <iostream>
using namespace std;
return 0;
}
b. #include <iostream>
using namespace std;
int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;
return 0;
}
c. #include <iostream>
using namespace std;
int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;
return 0;
}
13. Find any errors in the following C++ program and fix them:
#include <iostreem>
Int main(){
Int x,y;
Cin>>x,y;
Return k+j;
14. If x=10 and sum=0, what is the value of sum after each of the expressions?:
a.sum=++x b.sum=x++
16. If originally x=10 and y=5 what is the value of each of the expressions:
a.++x+y b.--x+y c.--x+(++y) d.--x+y—
18. if originally x=10,y=5, and z=2, evaluate the following expressions to true to false:
a.(x+1)!=y-2 b. !(x+y>20)
c.(x+1==y) || y-1>5 d.z+1>=3 || x+1<z*10 && y+3>7
19. If originally x=2,y=3, and z=5, what is the value of x,y, and z after executing the
following code?
if(x+1==y) y=y+1;
else x++;
20. If originally x=1,y=0, and z=1, what is the value of x, y, and z after executing the
following code?
if(x>y && x>z) {y=x;z=x+1;}
else if(x+y>=z) {x++;z=x+1;}
else y=z+x;
21. If originally x=1,y=2, and z=3, what is the value of x, y, and z after executing the
following code?
switch(x) {
case 0: x++;z=x+1;break;
case 1: y=z+x;break;
default: z=z+x;
22. Change the following C++ code from using a while loop to for loop:
x=1;
while(x<10){
cout<<x<<"\t";
<x<<"\t";
++x;
}</x<<"\t";