0% found this document useful (1 vote)
102 views

CPP Exercises

The document contains 22 coding problems and questions in C++. It includes problems involving declaring and printing variables, user input, finding maximum values, solving quadratic equations, temperature conversion, function definitions and calls, pattern generation, and evaluating conditional expressions. It also contains questions about modifying code snippets to fix errors or change loop structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
102 views

CPP Exercises

The document contains 22 coding problems and questions in C++. It includes problems involving declaring and printing variables, user input, finding maximum values, solving quadratic equations, temperature conversion, function definitions and calls, pattern generation, and evaluating conditional expressions. It also contains questions about modifying code snippets to fix errors or change loop structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a C++ program to declare two integer , one float variables and assign 10, 15, and 12.

6 to

them respectively. It then prints these values on the screen.

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

value of the three values.

Example:

Enter 3 integer vales separated by space: 10 15 20

The greatest value is: 20

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

roots of the equation based on the following rules:

-if a and b are zero=> no solution

-if a is zero=>one root (-c/b)

-if b2-4ac is negative=>no roots

-Otherwise=> two roots

The roots can be computed using the following formula:

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.

(You can use a function)

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.

8. What is the output of following programs (code fragments)?

1. int result = 4 + 5 * 6 + 2+(30/6); 2. int a = 5 + 7 % 2;

cout <<result;` cout<<a;

3. int x = 10, y;

y = x++;

cout << y;

4. int x = 10, y;

y = x++;

cout << x;

5. int x = 10;

cout << x++;


6. int x = 10,y;

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;

9. cout << setw(5) << 77 << endl;

cout << setw(5) << 100 << endl;

cout << setw(5) << 12312 << endl;

10. float net = 5689.2356;

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.

12. Write the output of the following programs :

a. #include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}
int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}
b. #include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

c. #include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

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>

Using name space std;

Int add(int& a,int& b);

Int main(){

Int x,y;

Cout “Please input two integers separated by space”;

Cin>>x,y;

Cout<<”The sum of the numbers is: “add(x,y);

float add(int k,int j(){

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++

15. If originally x=10 what is the value of each of the expressions:


a.--x b.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—

17. Evaluate the following expressions to true or false


a.3!=4-1 b. 5>=(1+6)-4 c.5+1==6 || 8-1>4 d.5>=6 || 1<8 && 9>7

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

You might also like