Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Assignment 5_C++ 1_Solution

C++

Uploaded by

os.nahswan77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 5_C++ 1_Solution

C++

Uploaded by

os.nahswan77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++

Semester 1 (2024-2025)

Due on 10-12-2024 Assignment # 5

1. (What Does this Program Do?) What does the following program print?

This Program prints the squares of the integer numbers from 1 to 10 and the sum of those
squares

1
2. (What Does this Program Do?) What does the following program print?

3. Write a program that displays the following checkerboard pattern. Your program
must use only three output statements, one of each of the following forms:

cout << "* ";


cout << ' ';
cout << endl;

2
4. Identify and correct the errors in each of the following:
a) while ( c <= 5 )
while ( c <= 5 )
(
{
product *= c;
product *= c;
++c;
++c;
)
}
must use { } instead of ( ) for while loop

b) cout >> value;


use << with cout instead of >>
cout << value;

c) if ( i == 1 )
cout << "A" << endl; if ( i == 1 ){
cout << "B" << endl; cout << "A" << endl;
else cout << "B" << endl;
cout << "C" << endl; }
else
missing { } for if statement cout << "C" << endl;

3
5. What’s wrong with the following while repetition statement?
while ( i <= 10 )
product *= i;

The value of the variable i is never changed in the while statement. Therefore, if the loop-continuation condition (i
<= 10) is initially true, an infinite loop is created

6. Write single C++ statements or portions of statements that do the following:


a) Input unsigned int variable x with cin and >>.
cin >> x
b) Input unsigned int variable y with cin and >>.
cin >> y;
c) Declare unsigned int variable i and initialize it to 1.
unsigned int i{1};
d) Declare unsigned int variable power and initialize it to 1.
unsigned int power{1};
e) Multiply variable power by x and assign the result to power.
power *= x;
f) Preincrement variable i by 1.
++i;
g) Determine whether i is less than or equal to y.
if (i <= y)
h) Output integer variable power with cout and <<.
cout << power << endl;

You might also like