0% found this document useful (0 votes)
22 views7 pages

C++ Programming Exercises and Outputs

Uploaded by

maryam osama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

C++ Programming Exercises and Outputs

Uploaded by

maryam osama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exercise 1:

Write a C++ program to sum the numbers started by 0 and increment by 2 for ten times using while
loop.

Exercise 2:
Write a C++ program to display the multiples of 3 backward from 33 to 3, inclusive using while loop.
Exercise 3:
Suppose that the input is: 32 53 -10 45 -56 -87 132 165 -999

What is the output of the following program?

#include <iostream>
using namespace std;
int main()
{
int num;
int count = 0;
cin >> num;
while (num != -999)
{
cout << num % (count + 1) << " ";
cin >> num;
count++;
}
cout << endl;
return 0;
}
Exercise 4:
Consider the following program.

#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int temp = 0;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << endl;
while ((num1 + num2) % 2 != (num1 + num2) % 3)
{
temp = num1 + num2;
num1 = num2;
num2 = temp;
cout << temp << " ";
}
cout << " *" << endl;
return 0;
}
a. What is the output if the input is 10 10?

b. What is the output if the input is -4 11?

c. What is the output if the input is 12 29?

d. What is the output if the input is 10 17?


Exercise 5:
Write a C++ program to print table of any no. using for loop.

Exercise 6:
Write a program that uses for loops to perform the following steps:

a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than
secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum
f. Output all uppercase letters.
Exercise 7:
Suppose that x, y, z, and w are int variables. What is stored in x, y, z, and w after the following
statements execute?
x = 9;
y = x - 4;
z = (y + 7) % 6;
w = (x * z) / y - 3;
z = w + (x - y + 2) % x;

Exercise 8:
Suppose x, y, and z are int variables and x = 18, y = 5, and z = 4. What is the output of each of the
following statements?
a. cout << "x = " << x << ", y = " << y<< ", z = " << z << endl;
b. cout << "5 * x - y = " << 5 * x - y << endl;
c. cout << "Product of " << x << " and " << z << " is "<< x * z << endl;
d. cout << "x - y / z = " << x - y / z << endl;
e. cout << x << " square = " << x * x << endl;
Exercise 9:
Suppose a and b are int variables, c is a double variable, and a = 25, b = 20, and c = 5.0. What is the
output of the following statements?
a. cout << a * 2 * b << endl;
b. cout << a + b / 2.0 + 1.5 * c << endl;
c. cout << a / static_cast<double>(b) << endl;
d. cout << 62 % 28 + a / c << endl;
e. cout << static_cast<int>(c) % 3 + 7 << endl;
f. cout << 22.5 / 2 + 14.0 * 3.5 + 28 << endl;
g. cout << 2 / (c - static_cast<int>(c + 1.2))<< endl;

Type Casting:

Exercise 10:
What will be the output of the following C++ Program?

#include <iostream>
using namespace std;
int main()
{
cout << "static_cast<int>(7.9) = "<< static_cast<int>(7.9)<< endl;
cout << "static_cast<int>(3.3) = "<< static_cast<int>(3.3)<< endl;
cout << "static_cast<double>(25) = "<< static_cast<double>(25)<< endl;
cout << "static_cast<double>(5 + 3) = "<< static_cast<double>(5 + 3)<< endl;
cout << "static_cast<double>(15) / 2 = "<< static_cast<double>(15) / 2<< endl;
cout << "static_cast<double>(15 / 2) = "<< static_cast<double>(15 / 2)<< endl;
cout << "static_cast<int>(7.8 + static_cast<double>(15) / 2) = "<< static_cast<int>(7.8 +
static_cast<double>(15) / 2)<< endl;

return 0;
}

Exercise 11:
What will be the Output of the following C++ Program?

#include <iostream>

using namespace std;

int main ()

int x = 10; int y = 15; int z = 0;

z = y + x++;

cout << "x = " << x << " y = " << y << " z = " << z << endl;

z = y++ + x;

cout << "x = " << x << " y = " << y << " z = " << z << endl;

z = y++ + x++;

cout << "x = " << x << " y = " << y << " z = " << z << endl;

z = --y + x++;

cout << "x = " << x << " y = " << y << " z = " << z << endl;

z = y + --x;

cout << "x = " + x << " y = " << y << " z = " << z << endl;

z = --y + --x;

cout << "x = " << x << " y = " << y << " z = " << z << endl;

Exercise 12:
Determine the output of the following program.

#include <iostream>

using namespace std;

int main()

int num1=15, num2=8, num3=0;

num3 = num1 + num2;

cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; cout << "num3 = " << num3 <<
endl;

num1 = num3 - num2++;

cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl;

cout << "num3 = " << num3 << endl;

num2 = num1-- + ++num3;

cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; cout << "num3 = " << num3 <<
endl;

Common questions

Powered by AI

The output, given integers input and requests to only print odd-indexed squares, leverages a for loop to traverse indices while conditionally checking and printing when indices mod 2 != 0. This control structure filters the required outputs efficiently within bounds stipulated, such as 1 to 10 for squaring but interleaves outputs controlled by modulo arithmetic.

The expression '5 * x - y' calculates as (5 * 18) - 5, which results in 90 - 5 = 85. This arithmetic follows simple operator precedence where multiplication precedes subtraction, leading to straightforward, intuitive calculations for final numerical outputs.

Casting 'static_cast<int>(7.9)' converts the floating-point to an integer by truncating the decimal part, resulting in 7. This exemplifies C++'s floor-like behavior in truncation versus rounding paradigms, evidencing handling precision lose-through conversion.

When the inputs are 10 and 17, the loop condition (num1 + num2) % 2 != (num1 + num2) % 3 continues until (10 + 17) % 2 == (10 + 17) % 3, which would be false immediately, leading to an output of “ *” since the loop does not execute even once.

In C++, post-increment increases the variable's value after its current use, while pre-increment changes it before use. In 'z = y + x++', x remains 10 for calculation then increments. 'z = y++ + x' results in y increasing after adding. Conversely, pre-increment includes the increase immediately as seen in '--y'. Their combination affects output incrementally.

For nested loops filtering with int bounds, initiate with 'for(int i = firstNum; i <= secondNum; i++)'. Use nested 'if' conditions as filters e.g., 'if(i % 2 == 0)' for even and 'else if(i % 2 != 0)' for odd, channeling respective output formats, enabling flexibility to possibly extend condition scenarios e.g., modular arithmetic expansions.

Implementing a for loop to output uppercase letters involves iterating from the ASCII value of 'A' (65) to 'Z' (90), using 'for(char c = 'A'; c <= 'Z'; c++) cout << c << ' ';'. This flows logically by exploiting consecutive ASCII ordering, ensuring complete character display for required range.

The program reads inputs and continues while numbers are not -999, calculating modulo with incrementing count. For 32 53 -10 45 -56 -87 132 165, the outputs show percent operations performed against count iterations, including edged behavior around negative handling and switch on directionality shifts, terminating neatly with a newline post-loop.

Initially, x = 9, y = 5, z = (5 + 7) % 6 = 1, w = (9 * 1) / 5 - 3 = -3. Then, z = -3 + (9 - 5 + 2) % 9 = -3 + 6 = 3. Hence, the final values are x = 9, y = 5, z = 3, w = -3.

Using 'static_cast' allows explicit type conversion in C++. For 'static_cast<int>(7.8 + static_cast<double>(15) / 2)', the expression evaluates '15 / 2' as a double (7.5) and adds it to 7.8, yielding 15.3. The 'static_cast<int>' then converts it to 15 by dropping the decimal part.

You might also like