0% found this document useful (0 votes)
28 views

Solved Questions

solved questions
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 (0 votes)
28 views

Solved Questions

solved questions
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/ 26

1. Write the output of the following segments of code.

a. int c = 1;
while (c < 20)
{
if (c%4 == 0 && c%3 != 0)
cout << c << "x"; c++;
}
4x8x16x
b. int d = 28;
while (d%2==0 || d > 1)
{
d = d / 2;
cout << d << "\n";
}
14
7
3
1

c. int x = 13;
int y = 3;
cout << x/y << y/x << x%y;

401
d. int x = 1;
while ( x < 9 )
{
x ++;
if ( x%2 == 1 )
cout << x << "+";
}

3+5+7+9+
e. int x = 7;
int y = 3;
cout << x/y << " and " << x%y;

2 and 1
f. int x = 3;
cout << x << 2*x;

36

Page 1 of 26
g. int x = 8;
while (x > 0) {
if (x%2 == 0)
x=x-3;
else
x--;
cout << x << "+";

}
5+4+1+0+

h. int b = 22;
do {
b /= 3;
cout << b << "+";

} while (b > = 1);

7+2+0+

i.

-1 0 3 8 15 24

j. int x = 19;
while (x > 2) {
x /= 2;
cout << x;
if (x%2 == 0)
cout << "\n";
}

94
2

Page 2 of 26
k. for (int i=10; i > 5; i--) {
if ( i %2 == 0)
cout << 2*i << "a";
else
cout << i << "b";
}
20a9b16a7b12a
l. int a[4];
a[0] = 2;
for (int i = 1; i < 4; i++)
a[i] = i + a[i-1];
for (int i = 0; i < 4; i++)
cout << a[i] << " ";
2358
m. for (int i=1; i<4; i++) {
for (int j=1; j<=i; j++)
cout << j << " ";
cout << "\n";
}
1
12
123

n. int a[5] = {10,3,5,1,2};


for (int i=4; i>0; i--) {
a[i] += a[i-1];
cout << a[i] << " ";
}

3 6 8 13
o. int a=4;
int j=1;
do {
cout << j << "+";
j = j + 2;
if (j>a)
j--;
} while (j<8);

1+3+4+5+6+7+

Page 3 of 26
2. Rewrite the for loop below as a do-while loop.

for (int x=0; x<100; x++)


cout << x << " ";

int x=0;
do {
cout << x << " ";
x++;
} while (x<100);

3. Rewrite the following do loop as a for loop. What does it print out?
int x = -10;
do {
cout << x << " & ";
x += 2;
} while (x < 100);

for (int x = -10; x < 100; x +=2) {


cout << x << " & ";
}
Prints out the even numbers -10 through 98 with an & in between.

4. Suppose we take as input an integer x.


int x;
cin >> x;
Write the if statement that runs under the specified condition. The first one is done for you as
an example.

a. x is a positive number
if ( x > 0 )
b. x is between 3 and 30, including 3 and 30
if (x >= 3 && x<=30)
c. x is an even number, not counting zero
if (x%2==0 && x!=0)
d. x is a number that ends in a zero, such as 420
if (x%10==0)

Page 4 of 26
5. Write a for loop that prints out the multiples of 3 up through 75, separated by a blank space.
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75
You do not need to write a full program.

for (int i=3; i<=75; i=i+3) cout << i << " ";
6. Write a portion of code (not a full program) that computes the sum of the even numbers 2
through 100.
2 + 4 + 6 + 8 + ... + 98 + 100
Store the value in a variable called sum. You do not have to print out anything.

int sum = 0;
int x = 2;
while (x <= 100) {
sum += x;
x = x+2;
}

7. Write a block of code (not a full program) that prints out all multiples of 5 up until 100,
separated by a space. Using a while loop .

int x = 5;
while (x <= 100) {
cout << x << " ";
x += 5;
}
8. The following blocks of code compile, but each contains a serious logic error. Explain the logic
error in each case. Do not correct the error, just describe it.
a. int x;
cin >> x;
if (x>0 || x<5)
cout << x;

With the or statement, this if statement would be true for all values of x. Every number is greather than
zero OR less than five.

b. int y = 1;
while (y <= 9) {
cout << y;
}

The variable y is never updated, so y=1 always. This is an infinite loop.

Page 5 of 26
9. Convert the following if/else sequence to a switch statement.

int x; int x;
cin >> x; cin >> x;
switch (x) {
if (x == 2)
case 2:
cout << "x=2"; cout << "x=2";
else if (x==3 || x==5) break;
{ case 3:
cout << "x=3 or 5"; case 5:
x++; cout << "x=3 or 5";
x++;
}
break;
else if (x==4) case 4:
cout << "x=4"; cout << "x=4"
else break;
cout << "Else."; default:
cout << "Else.";
break;
}

10. Write a program (starting from #include) that repeatedly collects positive integers from the
user, stopping when the user enters a negative number or zero. After that, output the largest
positive number entered. You may not use any library other than <iostream>. A sample run
should appear on the screen like the text below.

#include <iostream>
using namespace std;
int main ( ) {
int maxNum = 0;
int x = 1;
while ( x > 0 ) {
cout << "Enter a number: ";
cin >> x;
if ( x > maxNum && x > 0 )
maxNum = x;
}
cout << "The largest positive number you entered was " <<
maxNum << ".\n";
}
Page 6 of 26
11. Write a full program (starting from #include) that asks the user how many integer-valued
numbers she wants to input, reads the integers one at a time with an appropriate prompt, and
then outputs the average. An example output is shown below, where the user chose to enter 4
numbers.

#include <iostream>
using namespace std;
int main() {
cout << "How many numbers? ";
int num;
cin >> num;
int sum = 0;
int value;
int x = 1;
while (x <= num) {
cout << "Enter number #" << x << ": ";
cin >> value;
sum += value;
x++;
}
cout << "The average is " << (double) sum / num;
}

12. Explain the difference between the two expressions below. x = 2 and x == 2.
The left expression (x=2) assigns the value 2 to the variable x.
The right expression (x==2) is a boolean statement that checks if the value x equals 2.

13. Write a block of code that prints out the odd numbers 1 through 999. You do not need to write a
full program.

int x = 1;
while (x <= 999) {
cout << x << "\n";
x += 2;
}

Page 7 of 26
Page 8 of 26
14. Write a for loop that prints the given list of numbers, separated by one space. The first one is
done for you as an example.

1 2 3 4 for (int i=1; i <= 4; i++)


First four numbers. cout << i << " ";
-2 -3 -4 -5 -6 for (int i=-2; i>=-6; i--)
Counting down from -2 to -6 cout << i << " ";
2 4 8 16 32 64 for (int i=1; i<=6; i++)
Repeatedly doubles. cout << pow(2,i) << " ";
2 4 6 8 ... 998 1000 for (int i=2; i<=1000; i=i+2)
Even #s 2 through 1000. cout << i << " ";
Don't print "..."! OR
for (int i=1; i<=500; i++)
cout << 2*i << " ";

15. Write a program (starting from #include) that repeatedly collects positive integers from the
user, stopping when the user enters a negative number or zero. After that, output the product
of all positive entries. A sample run should appear on the screen like the text below.

# include <iostream>
using namespace std;
int main ( ) {
int x = 1;
int product = 1;
while ( x > 0 ) {
product *= x;
cout << "Enter a number: ";
cin >> x;
}
cout << "The product of all your positive numbers is "
<< product << ".\n";
}

Page 9 of 26
16. Correct the compile errors in the blocks of code below.
a. int x, y;
cin << x << y;
cout << "x = " << x << "\ny = " << y;

Arrows go wrong way.


cin >> x >> y;

b. int x;
cin >> x;
if (x%2 = 0)
cout << "You entered an even number.";

if (x%2 == 0)

c. if ( 2 < y < 10)


cout << "y is between 2 and 10";

if ( 2<y && y<10)

d. cout << "Two plus two is " 2+2;

cout << "Two plus two is " << 2+2;

e. if ( x = 1 );
cout << x;

if ( x == 1)

f. if ( x = 1 or 2 )
cout << x;

if ( x == 1 || x == 2 )

g. int x = 1;
while ( x < 10 );
cout << x;
x ++;

while ( x < 10 ) {
}

Page 10 of 26
h. if ( 1 < x < 5 )
cout << x;

if ( x > 1 && x < 5 )

i. int a = 5;
int double = 2*a;
int triple = 3*a;
cout << "Doubling 5 gives " << double << " and tripling gives " << triple;

Can't use double as variable name. Change it to my_double.

j. char c;
cin >> c;
if (c=="f" && c=="F")
cout << "You typed an upper or lower-case F.";

Will never run because can't be both f and F. Also chars have single quotes.
if (c=='f' || c=='F')

Page 11 of 26
17. Write a full program (starting from #include) that takes as input the number of seconds after
midnight. It then displays the time in hours:minutes:seconds format. Assume the time is
displayed in military time, e.g. 06:06:06 or 23:05:57. Note the placement of the zeros for
numbers less than 10, which your program should properly display. Your program should show
output as in the example below.

#include <iostream>
using namespace std;
int main ( ) {
int seconds;
cout << "Enter number of seconds after midnight: ";
cin >> seconds;
int hours = seconds / 3600;
int mins = (seconds-hours*3600)/60;
int secs = (seconds-hours*3600)%60;
cout << "The time is ";
if (hours < 10)
cout << "0";
cout << hours << ":";
if (mins < 10)
cout << "0";
cout << mins << ":";
if (secs < 10)
cout << "0";
cout << secs << "\n";
}

Page 12 of 26
18. The Euclidean norm of a n-dimensional vector x is defined as

Write a a full program (starting from #include)a vector of 20 doubles as input and returns the
norm. You may assume the <cmath> library is available. An example is shown below.

#include <iostream>
#include <cmath>
using namespace std;
int main ( ) {

double a[20];
double sum , norm

for ( int k=0 ; k<20;k++)


{
cout <<’’Enter value no. ‘‘<< k+1<<endl;
cin << a[k]
}
for (int i=0; i<20; i++)
{
sum += a[i]*b[i];
}

norm = sqrt(sum);

cout << ‘‘Norm = ‘‘ << norm ;

Page 13 of 26
19. Suppose that sale and bonus are double variables. Write an if. . .else statement that
assigns a value to bonus as follows: If sale is greater than $20,000, the value assigned to
bonus is 0.10; If sale is greater than $10,000 and less than or equal to $20,000, the value
assigned to bonus is 0.05; otherwise, the value assigned to bonus is 0.

#include <iostream>

using namespace std;


int main()

{
double sale, bonus;

cout << "Enter the sales value \n";


cin >> sale ;
if ( sale > 20000)
bonus=0.10;
else if( sale > 10000 && sale <= 200000)
bonus=0.05;
else
bonus=0;
cout << <<bonus <<endl;
}

Page 14 of 26
20. Suppose that overSpeed and fine are double variables. Assign the value to fine as
follows: If 0 < overSpeed <= 5, the value assigned to fine is $20.00; if 5 < overSpeed <=
10, the value assigned to fine is $75.00 if 10 < overSpeed <= 15, the value assigned to
fine is $150.00; if overSpeed > 15, the value assigned to fine is $150.00 plus $20.00 per
mile over 15.

#include <iostream>
using namespace std;
int main()
{
double overSpeed , fine ;

cout << "Enter the over Speed value \n";


cin >> overSpeed ;

if( overSpeed > 0 && overSpeed <= 5)


fine= 0;
else if( overSpeed > 5 && overSpeed <= 10)
fine= 20;
else if( overSpeed > 10 && overSpeed <= 15)
fine= 75;
else if( overSpeed > 15)
fine= 150 + (overSpeed-15)*20;
cout << "The Fine is "<< fine <<"$" <<endl;
}

Page 15 of 26
21. Suppose the input is 5. What is the value of alpha after the following C++ code executes?

cin >> alpha;


switch (alpha)
{
case 1:
case 2:
alpha = alpha + 2;
break;
case 4:
alpha++;
case 5:
alpha = 2 * alpha;
case 6:
alpha = alpha + 5;
break;
default:
alpha--;
}

15

22. Suppose the input is 6. What is the value of a after the following C++ code executes?

cin >> a;
if (a > 0)
switch (a)
{
case 1:
a = a + 3;
case 3:
a++;
break;
case 6:
a = a + 6;
case 8:
a = a * 8;
break;
default:
a--;
}
else
a = a + 2;

96

Page 16 of 26
23. Write the missing statements in the following program so that it prompts the user to input
two numbers. If one of the numbers is 0, the program should output a message indicating
that both numbers must be nonzero. If the first number is greater than the second number,
it outputs the first number divided by the second number; if the first number is less than
the second number, it outputs the second number divided by the first number; otherwise,
it outputs the product of the numbers.

#include <iostream>
using namespace std;
int main()
{
double FirstNum , SecondNum;
cout << "Enter two non zero numbers :";
cin >> FirstNum >> SecondNum ;
cout << endl;
double result=0;
if(FirstNum == 0 || SecondNum == 0)
cout << "Both numbers must be nonzero";
else if(FirstNum > SecondNum)
result= FirstNum/SecondNum;
else if (FirstNum < SecondNum)
result= SecondNum/FirstNum;
else
result= FirstNum*SecondNum;

cout << " the result is " << result;


}

Page 17 of 26
24. Write a program that prompts the user to input a number. The program should then output
the number and a message saying whether the number is positive, negative, or zero.

#include <iostream>
using namespace std;
int main()
{
double Num ;
cout << "Enter The number:";
cin >> Num ;
if(Num == 0)
cout << "The number " << Num <<" is zero";
else if(Num > 1)
cout << "The number " << Num <<" is Positive";
else
cout << "The number " << Num <<" is Negative";
}

Page 18 of 26
25. The following program contains errors. Correct them so that the program will run and
output w = 21.

#include <iostream>
using namespace std;
int main ()
{
int SECRET = 5;
int x, y, w, z;
z = 9;
if (z > 10)
{
x = 12;
y = 5;
w = x + y + SECRET;
}

else
{
x = 12;
y = 4;
w = x + y + SECRET;
}

cout << "w = " << w << endl;


}

Page 19 of 26
26. Write a program that prompts the user to input three numbers. The program should then
output the numbers in ascending order.
#include <iostream>
using namespace std;
int main()
{
int x, y, z;
cout<<"Enter 3 numbers: ";
cin>>x>>y>>z;
cout<<"Numbers in ascending order: ";
if(x<y)
{
/*x is less than y*/
if(x<z)
{
/* x is less than y AND z => x is smallest - print it*/
cout<<x<<", ";
/* comparing z and y now, x has already been dealt with*/
if(z<y)
cout<<z<<", "<<y<<endl;
else
cout<<y<<", "<<z<<endl;
}
else
{
/* x is less than y, but z is still lesser => z is least - print it*/
cout<<z<<", ";
/*now deal with reamining x and y*/
if(x<y)
cout<<x<<", "<<y<<endl;
else
cout<<y<<", "<<x<<endl;
}
}
else
{
/*y is less than x*/
if(y<z)
{
/*y is less than x AND z => y is least, print it*/
cout<<y<<", ";
/*deal with other two i.e. x and z*/
if(x<z)
cout<<x<<", "<<z<<endl;
else
cout<<z<<", "<<x<<endl;
}
else
{
/*y is less than x, but z is still lesser => z is least, print it*/
cout<<z<<", ";
/*deal with remaining x & y*/
if(x<y)
cout<<x<<", "<<z<<endl;
else cout<<y<<", "<<x<<endl;
}
}
}
Page 20 of 26
27. The roots of the quadratic equation ax2 + bx + c = 0, a ≠ 0 are given by the following
formula:
−𝑏 ± √𝑏2 − 4𝑎𝑐
𝑥=
2𝑎
In this formula, the term 𝑏2 − 4𝑎𝑐 is called the discriminant. If 𝑏2 − 4𝑎𝑐 = 0, then the
equation has a single (repeated) root. If 𝑏2 − 4𝑎𝑐 > 0, the equation has two real roots. If
𝑏2 − 4𝑎𝑐 <0, the equation has two complex roots. Write a program that prompts the user
to input the value of a (the coefficient of x2), b (the coefficient of x), and c (the constant
term) and outputs the type of roots of the equation. Furthermore, if 𝑏2 − 4𝑎𝑐 ≥ 0 , the
program should output the roots of the quadratic equation. (Hint: Use the function pow
from the header file cmath to calculate the square root.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c,discriminant,root1 ,root2;
cout << " Enter the value of a \n";
cin >> a ;
cout << " Enter the value of b \n";
cin >> b ;
cout << " Enter the value of c \n";
cin >> c ;
discriminant = (b*b)- (4* a * c);

if ( discriminant < 0 )
{
cout << " The equation has two complex roots\n" ;
}
else if ( discriminant > 0 )
{
cout << " The equation has two real roots\n" ;
root1 = (-b + pow(discriminant,0.5))/(2*a);
root2 = (-b - pow(discriminant,0.5))/(2*a);
cout << "root1= "<< root1 << "root2= " << root2<<endl;

}
else
{
cout << " the equation has a single (repeated) roots\n" ;
root1 = (-b + pow(discriminant,0.5))/(2*a);
root2 = (-b - pow(discriminant,0.5))/(2*a);
cout << "root1= "<< root1 << "root2= " << root2<<endl;

Page 21 of 26
28. How many times will each of the following loops execute? What is the output in each case?

Page 22 of 26
29. One way to determine how healthy a person is by measuring the body fat of the person.
The formulas to determine the body fat for female and male are as follows:

Body fat formula for women:

Write a program to calculate the body fat of a person.

#include <iostream>
using namespace std;
int main()
{
double weight , a1 , a2 , a3, a4 , a5 ,b , body_fat , body_fat_percent,
wrist, waist , forearm ,hip ;
char s;

cout << " Please Enter m for male and f for female \n" ;
cin >> s;
switch (s) {
case 'm':
{
cout << "hello Sir Please enter your weight \n";
cin >> weight;
cout << "Please enter your wrist measurment \n";
cin >> wrist;
a1 = ( weight * 1.082) + 94.42;
a2 = wrist * 4.15;
b=a1-a2;
body_fat = weight - b;
body_fat_percent= (body_fat * 100) / weight ;
cout << "your boady fat is " << body_fat<<endl;
cout << "your boady fat percentage is " << body_fat_percent<<endl;

break;
}
Page 23 of 26
case 'f' :
{
cout << "hello ma'am Please enter your weight \n";
cin >> weight;
cout << "Please enter your wrist measurment \n";
cin >> wrist;
cout << "Please enter your waist measurment \n";
cin >> waist;
cout << "Please enter your hip measurment \n";
cin >> hip;
cout << "Please enter your forearm measurment \n";
cin >> forearm;
a1 = ( weight * 0.732) + 8.987;
a2 = wrist / 3.14;
a3 = waist * 0.157;
a4 = hip * 0.249;
a5 = forearm *.434;
b=a1+a2-a3-a4+a5;

body_fat = weight - b;
body_fat_percent= (body_fat * 100) / weight ;
cout << "your boady fat is " << body_fat<<endl;
cout << "your boady fat percentage is " << body_fat_percent<< "%"<<endl;
break;
}
default:
cout << "invalid selection \n";
}

Page 24 of 26
30. Write a program that reads a set 20 of integers and then finds and prints the sum and
count of the even and odd integers.

#include <iostream>
using namespace std;
int main()
{
/*. Write a program that reads a set 20 of integers and then finds and
prints the sum and count of the even and odd integers */

int value , odd_count = 0 ,even_count = 0 ,even_sum = 0 ,odd_sum =0 ;

for ( int i = 1 ; i<=20 ; i++)


{
cout << "enter the value u want to test \n";
cin >> value ;
if ( value % 2 == 0 )
{
even_sum = even_sum + value ;
even_count++;
}
else
{
odd_sum = odd_sum + value ;
odd_count++;
}
}

cout << " you entered "<< even_count << " even numbers with total sum of
" << even_sum << endl ;

cout << " you entered "<< odd_count << " odd numbers with total sum of "
<< odd_sum << endl ;

31. What is the output of the following program?


#include <iostream>
using namespace std;
int main()
{
int x, y, z;
x = 4; y = 5;
z = y + 6;
do
{
cout << z << " ";
z = z + 7;
}while (((z - x) % 4) != 0);
cout << endl;
}

11 18 25

Page 25 of 26
32. Consider the following declaration:
double salary[10];
In this declaration, identify the following:
a. The array name. salary
b. The array size. 10
c. The data type of each array component. double
d. The range of values for the index of the array. 0 to 9

33. Write C++ statements to do the following:


a. Declare an array alpha of 15 components of type int.

int alpha[15]

b. Output the value of the tenth component of the array alpha.

cout << alpha[9];

c. Set the value of the fifth component of the array alpha to 35.

alpha[4]= 35 ;

d. Set the value of the ninth component of the array alpha to the sum of the sixth and
thirteenth components of the array alpha.
alpha[8]= alpha[5]+ alpha[12] ;

e. Set the value of the fourth component of the array alpha to three times the value
of the eighth component minus 57.

alpha[3]= (3 * alpha[7]) - 57;

34.

-3 -1 1 3 5
5 -1 8 3 -1

Page 26 of 26

You might also like