0% found this document useful (0 votes)
213 views132 pages

Prac1sol PDF

The document contains 13 programming problems that involve writing C++ programs to: 1. Read an integer input from the user and check if it is in a given range 2. Print various patterns or determine properties of numbers based on the input 3. The programs print sample outputs showing expected behavior for given integer inputs

Uploaded by

fortino_sanchez
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)
213 views132 pages

Prac1sol PDF

The document contains 13 programming problems that involve writing C++ programs to: 1. Read an integer input from the user and check if it is in a given range 2. Print various patterns or determine properties of numbers based on the input 3. The programs print sample outputs showing expected behavior for given integer inputs

Uploaded by

fortino_sanchez
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/ 132

Problem 1

Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 21.
2. It repeatedly reads n from the user until the supplied value of n is legal.
3. It prints out a picture of an n n square formed of X characters except that a diagonal stripe, formed by the
diagonal and any position immediately to its right, is printed using an O character.
Here is an example of how the program should work:
Give me an integer between 1 and 21:
OOXXX
XOOXX
XXOOX
XXXOO
XXXXO

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 21:";
cin >> n;
while (n < 1 || n > 21) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if ((c == r) || (c == r + 1)) cout << "O";
else cout << "X";
cout << endl;
}
return 0;
}

Problem 2
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 15.
2. It terminates at once if the user enters an illegal value for n.
3. It prints out a picture using (+ signs) of a diagonal line that extends over n rows and has a width of 3 characters
in each row.
Here is an example of how the program should work:
Give me an integer between 1 and 15:
+++
+++
+++
+++
+++
+++
Answer:

#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 15:";
cin >> n;
if (n < 1 || n > 15) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n + 2; c++)
if ((c >= r) && (c <= (r + 2))) cout << "+";
else cout << " ";
cout << endl;
}
return 0;
}
Problem 3
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 15.
2. It repeatedly reads n from the user until the supplied value of n is legal.
3. It prints out a picture of an n n square formed of O characters except that a diagonal stripe, formed by the
diagonal and any position immediately to its left, is left blank.
Here is an example of how the program should work:
Give me an integer between 1 and 15:
OOOOO
OOOO
O OOO
OO OO
OOO O
OOOO

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 15:";
cin >> n;
while (n < 1 || n > 15) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if ((c == r) || (c == r - 1)) cout << " ";
else cout << "O";
cout << endl;
}
return 0;
}

Problem 4
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 21.
2. It terminates at once if the user enters an illegal value for n.
3. It prints out a picture using (+ signs) of left sloping diagonal line with length n.
Here is an example of how the program should work:
Give me an integer between 1 and 21:
+
+
+
+
+

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 21:";
cin >> n;
if (n < 1 || n > 21) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if ( (c + r) == (n + 1)) cout << "+";
else cout << " ";
cout << endl;
}
return 0;
}

Problem 5
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 9.
2. It repeatedly reads n from the user until the supplied value of n is legal.
3. It prints out a picture of a triangle with n rows, in which the symbol used to print each row is the rows number.
Here is an example of how the program should work:
Give me an integer between 1 and 9:
1
22
333
4444
55555
Answer:
#include <iostream>
using namespace std;
int main() {
int n;

cout << "Give me an integer between 1 and 9:";


cin >> n;
while (n < 1 || n > 9) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++) cout << r;
cout << endl;
}
return 0;
}
Problem 6
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 9.
2. It exits immediately if n is illegal.
3. It prints out a picture of a triangle with n rows, in which the symbol used to print each column is the columns
number.
Here is an example of how the program should work:
Give me an integer between 1 and 9:
1
12
123
1234
12345

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
if (n < 1 || n > 9) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++) cout << c;
cout << endl;
}
return 0;
}
Problem 7
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 9.
2. It repeatedly reads n from the user until the supplied value of n is legal.
3. It prints out a picture of an upside down triangle with n rows, in which the symbol used to print each row is the
rows number.
Here is an example of how the program should work:

Give me an integer between 1 and 9:


11111
2222
333
44
5

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
while (n < 1 || n > 9) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (int r = n; r >= 1; r--) {
for (int c = 1; c <= r; c++) cout << n + 1 - r;
cout << endl;
}
return 0;
}

Problem 8
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is between 1 and 9.
2. It exits immediately if n is illegal.
3. It prints out a picture of an upside down triangle with n rows, in which the symbol used to print each column is
the columns number.
Here is an example of how the program should work:
Give me an integer between 1 and 9:
12345
1234
123
12
1

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
if (n < 1 || n > 9) return 0;
for (int r = n; r >= 1; r--) {

for (int c = 1; c <= r; c++) cout << c;


cout << endl;
}
return 0;
}

Problem 9
A number is called upward if its last digit is greater than the previous digit. Write a complete C++
program that does the following. (Programs that correctly carry out some of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 9.
2. It terminates when given illegal input.
3. It prints out whether n is upward.
Here is an example of how the program should work:
Give me an integer greater than 9:

95424

Upward
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 9:";
cin >> n;
if (n < 10) return 0;
int lastDigit = n % 10;
n = n / 10;
int nextLast = n % 10;
if (lastDigit > nextLast) cout << "Upward\n";
else cout << "Not upward\n";
return 0;
}

Problem 10
A number is called evil if its last two digits add to 13. Write a complete C++ program that does
the following. (Programs that correctly carry out some of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 666.
2. It terminates when given illegal input.
3. It prints out whether n is evil.
Here is an example of how the program should work:
Give me an integer greater than 666: 667
Evil
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 666:";

cin >> n;
if (n <= 666) return 0;
int lastDigit = n % 10;
n = n / 10;
int nextLast = n % 10;
if ((lastDigit + nextLast) == 13) cout << "Evil\n";
else cout << "Not evil\n";
return 0;
}

Problem 11
A number is called flat if its last two digits are equal. Write a complete C++ program that does
the following. (Programs that correctly carry out some of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 9.
2. It terminates when given illegal input.
3. It prints out whether n is flat.
Here is an example of how the program should work:
Give me an integer greater than 9:

95424

Not flat
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 9:";
cin >> n;
if (n < 10) return 0;
int lastDigit = n % 10;
n = n / 10;
int nextLast = n % 10;
if (lastDigit == nextLast) cout << "Flat\n";
else cout << "Not flat\n";
return 0;
}

Problem 12
A number is called lucky if the product of its last two digits ends in a 3. Write a complete C++
program that does the following. (Programs that correctly carry out some of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 666.
2. It terminates when given illegal input.
3. It prints out whether n is lucky.
Here is an example of how the program should work:
Give me an integer greater than 666: 697
Lucky
Answer:

#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 666:";
cin >> n;
if (n <= 666) return 0;
int lastDigit = n % 10;
n = n / 10;
int nextLast = n % 10;
if ((lastDigit * nextLast) % 10 == 3)
cout << "Lucky\n";
else cout << "Not lucky\n";
return 0;
}

Problem 13
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 9.
2. It terminates when given illegal input.
3. It prints out the first 2 digits of n (in order, on one line).
Here is an example of how the program should work:
Give me an integer greater than 9:

95424

95
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 9:";
cin >> n;
if (n < 10) return 0;
while (n > 99) n = n / 10;
cout << n << endl;
return 0;
}

Problem 14
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer n.
2. It terminates when given illegal input.
3. It prints out the product of the digits of n.
Here is an example of how the program should work:
Give me a positive integer:
12

41311

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me a positive integer:";
cin >> n;
if (n <= 0) return 0;
int product = 1;
while (n > 0) {
product = product * (n % 10);
n = n / 10;
}
cout << product << endl;
return 0;
}
Problem 15
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer n that is greater than 99.
2. It terminates when given illegal input.
3. It prints out the first 3 digits of n (in order, on one line).
Here is an example of how the program should work:
Give me an integer greater than 99:

95424

954
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 99:";
cin >> n;
if (n < 100) return 0;
while (n > 999) n = n / 10;
cout << n << endl;
return 0;
}
Problem 16
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer n.
2. It terminates when given illegal input.
3. It prints out the sum of those digits of n that are even numbers.
Here is an example of how the program should work:
Give me a positive integer:
12

41815

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me a positive integer:";
cin >> n;
if (n <= 0) return 0;
int sum = 0;
while (n > 0) {
int digit = n % 10;
if (digit % 2 == 0) sum = sum + digit;
n = n / 10;
}
cout << sum << endl;
return 0;
}

Problem 17

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 123;
cout << x % 10 << endl;
cout << x / 10 << endl;
if (x > 50) cout << "Big" << endl;
cout << endl;
while (x > 0) { cout << "1"; x /= 10;}
cout << endl;
cout << x << endl;
}
(a) What is the output at line (a)?
Answer:
3
(b) What is the output at line (b)?
Answer:
12
(c) What is the output at line (c)?
Answer:
Big

(d) What is the output at line (d)?


Answer:
111

// line (a)
// line (b)
// line (c)
// line (d)
// line (e)

(e) What is the output at line (e)?


Answer:
0
Problem 18

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 2345;
cout << x % 10 << endl;
cout << x / 10 << endl;
if (x > 5000) cout << "Big" << endl;
cout << endl;
while (x > 0) { cout << "*"; x /= 10;}
cout << endl;
cout << x + 5 << endl;
}

// line (a)
// line (b)
// line (c)
// line (d)
// line (e)

(a) What is the output at line (a)?


Answer:
5
(b) What is the output at line (b)?
Answer:
234
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:
****
(e) What is the output at line (e)?
Answer:
5
Problem 19

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 31;
cout << x % 10 << endl;
cout << x / 10 << endl;
if (x > 50) cout << "Big" << endl;
cout << endl;
while (x > 0) { cout << "1"; x /= 10;}
cout << endl;
cout << x * x << endl;
}

// line (a)
// line (b)
// line (c)
// line (d)
// line (e)

(a) What is the output at line (a)?


Answer:
1
(b) What is the output at line (b)?
Answer:
3
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:
11
(e) What is the output at line (e)?
Answer:
0

Problem 20

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 5432;
cout << x % 10 << endl;
cout << x / 10 << endl;
if (x > 5000) cout << "Big" << endl;
cout << endl;
while (x > 0) { cout << "A"; x /= 10;}
cout << endl;
cout << x - 5 << endl;
}
(a) What is the output at line (a)?
Answer:
2
(b) What is the output at line (b)?
Answer:
543
(c) What is the output at line (c)?
Answer:
Big

// line (a)
// line (b)
// line (c)
// line (d)
// line (e)

(d) What is the output at line (d)?


Answer:
AAAA
(e) What is the output at line (e)?
Answer:
-5

Problem 21

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int y,x = 12;
cout << x + x * 10 << endl;
//
cout << x / 100 << endl;
//
for (y = 10; y < x; y++) cout << y;
//
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; //
cout << endl;
cout << x << "*" << x << endl;
//
}
(a) What is the output at line (a)?
Answer:
132
(b) What is the output at line (b)?
Answer:
0
(c) What is the output at line (c)?
Answer:
1011
(d) What is the output at line (d)?
Answer:
24
(e) What is the output at line (e)?
Answer:
12*12

Problem 22

Consider the following C++ program.

line (a)
line (b)
line (c)
line (d)
line (e)

#include <iostream>
using namespace std;
int main() {
int y,x = 210;
cout << x + x * 10 << endl;
//
cout << x / 100 << endl;
//
for (y = 210; y < x; y++) cout << y;
//
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; //
cout << endl;
cout << x << "*" << x << endl;
//
}

line (a)
line (b)
line (c)
line (d)
line (e)

(a) What is the output at line (a)?


Answer:
2310
(b) What is the output at line (b)?
Answer:
2
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:
210
(e) What is the output at line (e)?
Answer:
210*210

Problem 23

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int y,x = 13;
cout << x + x * 10 << endl;
//
cout << x / 100 << endl;
//
for (y = 10; y < x; y++) cout << y;
//
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; //
cout << endl;
cout << x << "*" << x << endl;
//
}
(a) What is the output at line (a)?
Answer:

line (a)
line (b)
line (c)
line (d)
line (e)

143
(b) What is the output at line (b)?
Answer:
0
(c) What is the output at line (c)?
Answer:
101112
(d) What is the output at line (d)?
Answer:
26
(e) What is the output at line (e)?
Answer:
13*13

Problem 24

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int y,x = 211;
cout << x + x * 10 << endl;
//
cout << x / 100 << endl;
//
for (y = 210; y < x; y++) cout << y;
//
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; //
cout << endl;
cout << x << "*" << x << endl;
//
}
(a) What is the output at line (a)?
Answer:
2321
(b) What is the output at line (b)?
Answer:
2
(c) What is the output at line (c)?
Answer:
210
(d) What is the output at line (d)?
Answer:

line (a)
line (b)
line (c)
line (d)
line (e)

211
(e) What is the output at line (e)?
Answer:
211*211

Problem 25
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y;

(a) Print y copies of the word Hello on a single line of output.


Answer:
for (int c = 1; c <= y; c++) cout << "Hello ";
cout << endl;

(b) Print the value of x as a percentage of y, with output like 75.0%.


Answer:
cout << 100.0 * x / y << "%" << endl;

(c) Read new values for x and y from the user.


Answer:
cin >> x >> y;

(d) Replace y by its absolute value.


Answer:
if (y < 0) y = -y;

(e) Print the first digit of y


Answer:
while (y >= 10) {
y = y / 10;
}
cout << y << endl;

Problem 26
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.

int x, y;

(a) Print x copies of the number y on a single line of output.


Answer:
for (int c = 1; c <= x; c++) cout << y;
cout << endl;

(b) Print the value of y as a percentage of x, with output like 75.0%.


Answer:
cout << 100.0 * y / x << "%" << endl;

(c) Read a new value for y and then for x from the user.
Answer:
cin >> y >> x;

(d) Replace y by the absolute value of x - y.


Answer:
y = x - y;
if (y < 0) y = -y;

(e) If y is greater than 10, print the second digit of y


Answer:
while (y > 100) {
y = y / 10;
}
if (y > 10) cout << y % 10 << endl;

Problem 27
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y;

(a) Read new values for x and y from the user.


Answer:
cin >> x >> y;

(b) Print the value of x as a percentage of y, with output like 75.0%.


Answer:
cout << 100.0 * x / y << "%" << endl;

(c) Print x copies of the word cin on a single line of output.


Answer:
for (int c = 1; c <= y; c++) cout << "cin";
cout << endl;

(d) Replace y by the absolute value of x.


Answer:
if (x < 0) y = -x;
else y = x;

(e) Print the first digit of y


Answer:
while (y >= 10) {
y = y / 10;
}
cout << y << endl;

Problem 28
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y;

(a) Read a new value for y and then for x from the user.
Answer:
cin >> y >> x;

(b) Print y copies of the number x on a single line of output.


Answer:
for (int c = 1; c <= y; c++) cout << x;
cout << endl;

(c) Print 75.0% on a single line.


Answer:

cout << "75.0%" << endl;

(d) Replace y by the absolute value of - x - y.


Answer:
y = - x - y;
if (y < 0) y = -y;

(e) If y is greater than 10, print the second digit of y


Answer:
while (y > 100) {
y = y / 10;
}
if (y > 10) cout << y % 10 << endl;

Problem 29
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized, and x is positive
int x, y;

(a) Print x copies of the last digit of x on a single line of output.


Answer:
for (int c = 1; c <= x; c++) cout << x % 10;
cout << endl;

(b) Print the exact value the quotient of x by y, as a decimal


Answer:
cout << (1.0 * x) / y << endl;

(c) Exit the program if y is negative


Answer:
if (y < 0) return 0;

(d) Replace y by its absolute value.


Answer:
if (y < 0) y = -y;

(e) Print the first digit of x


Answer:
while (x >= 10) {
x = x / 10;
}
cout << x << endl;

Problem 30
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized, and x is positive
int x, y;

(a) Exit the program if y is positive


Answer:
if (y > 0) return 0;

(b) Print -y copies of the the string "y >= 0; "


Answer:
for (int c = 1; c <= -y; c++) cout << "y >= 0; " <<

(c) Print the exact value the quotient of y by x, as a decimal


Answer:
cout << (1.0 * y) / x << endl;

(d) Replace y by the absolute value of x + y.


Answer:
y = x + y;
if (y < 0) y = -y;

(e) Print the first digit of y


Answer:
while (y >= 10) {
y = y / 10;
}
cout << y << endl;

endl;

Problem 31
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized, and x is positive
int x, y;

(a) Exit the program if y is negative


Answer:
if (y < 0) return 0;

(b) Print x copies of the last digit of y on a single line of output.


Answer:
for (int c = 1; c <= x; c++) cout << y % 10;
cout << endl;

(c) Print the exact value the quotient of x by y, as a decimal


Answer:
cout << (1.0 * x) / y << endl;

(d) Replace y by its absolute value.


Answer:
if (y < 0) y = -y;

(e) Print the first digit of x followed by the last digit of y


Answer:
while (x >= 10) {
x = x / 10;
}
cout << x << y% 10 << endl;

Problem 32
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized, and x is positive
int x, y;

(a) Print the exact value the quotient of y by x, as a decimal


Answer:

cout << (1.0 * y) / x << endl;

(b) Exit the program if y is positive


Answer:
if (y > 0) return 0;

(c) Print -y copies of the the string "y >= 0; "


Answer:
for (int c = 1; c <= -y; c++) cout << "y >= 0; " <<

endl;

(d) Replace y by the absolute value of x - y.


Answer:
y = x - y;
if (y < 0) y = -y;

(e) Print the first digit of y followed by the last digit of x


Answer:
while (y >= 10) {
y = y / 10;
}
cout << y << x % 10 << endl;

Problem 33
Write a complete C++ program that does the following. The program prints a rectangular pattern
of Os and Xs. The pattern has 200 rows and 50 columns. Every third row is made of Os and the other rows are
made of Xs.
For example, a similar pattern with 7 rows and 5 columns would appear as follows.
XXXXX
XXXXX
OOOOO
XXXXX
XXXXX
OOOOO
XXXXX
Answer:
#include <iostream>
using namespace std;
int main() {
for (int r = 1; r <= 200; r++) {
for (int c = 1; c <= 50; c++) {
if ((r % 3) == 0) cout << "O";

else cout << "X";


}
cout << endl;
}
return 0;
}

Problem 34
Write a complete C++ program that does the following. The program prints a rectangular pattern
of Os and Xs. The pattern has 50 rows and 60 columns. Every fourth column is made of Os and the other columns
are made of Xs.
For example, a similar pattern with 7 rows and 9 columns would appear as follows.
XXXOXXXOX
XXXOXXXOX
XXXOXXXOX
XXXOXXXOX
XXXOXXXOX
XXXOXXXOX
XXXOXXXOX
Answer:
#include <iostream>
using namespace std;
int main() {
for (int r = 1; r <= 50; r++) {
for (int c = 1; c <= 60; c++) {
if ((c % 4) == 0) cout << "O";
else cout << "X";
}
cout << endl;
}
return 0;
}

Problem 35
Write a complete C++ program that does the following. The program prints a rectangular pattern
of Os and Xs. The pattern has 101 rows and 51 columns. The middle row and column are made of Os but all other
entries in the pattern are Xs.
For example, a similar pattern with 7 rows and 5 columns would appear as follows.
XXOXX
XXOXX
XXOXX
OOOOO
XXOXX
XXOXX
XXOXX
Answer:
#include <iostream>
using namespace std;
int main() {
for (int r = 1; r <= 101; r++) {

for (int c = 1; c <= 51; c++) {


if ((r == 51) || (c == 26)) cout << "O";
else cout << "X";
}
cout << endl;
}
return 0;
}

Problem 36
Write a complete C++ program that does the following. The program prints a square pattern of
Os and Xs. The pattern has 52 rows and 52 columns. The entries on the two diagonals of the square are made of
Os but all other entries in the pattern are Xs.
For example, a similar pattern with 7 rows and 7 columns would appear as follows.
OXXXXXO
XOXXXOX
XXOXOXX
XXXOXXX
XXOXOXX
XOXXXOX
OXXXXXO
Answer:
#include <iostream>
using namespace std;
int main() {
for (int r = 1; r <= 52; r++) {
for (int c = 1; c <= 52; c++) {
if ((r == c) || ((c + r) == 53)) cout << "O";
else cout << "X";
}
cout << endl;
}
return 0;
}

Problem 37
Write a complete C++ program that does the following. The user is given 3 chances to enter a
secret password which is FRED. If the user fails the program terminates, otherwise it says Hello. Here is a sample
run.
What is the password? Freddy
Wrong. Try again: Fred
Wrong. Try again: FRED
Hello.
Answer:
#include <iostream>
using namespace std;
int main() {
string guess;
int strikes = 0;
cout << "What is the password? ";

cin >> guess;


while (guess != "FRED") {
strikes++;
if (strikes >= 3) return 0;
cout << "Wrong. Try again: ";
cin >> guess;
}
cout << "Hello." << endl;
return 0;
}

Problem 38
Write a complete C++ program that does the following. The user is given 3 chances to guess a
secret number, which is 7. Whenever the user makes an incorrect guess, the program announces whether the guess
was too big or too small. If the user succeeds, the program says Congratulations! otherwise it says Sorry. The
secret number is 7. Here is a sample run.
Can you guess my number? 10
Too big. Try again: 5
Too small. Try again: 8
Sorry. The secret number is 7.
Answer:
#include <iostream>
using namespace std;
int main() {
int guess;
int strikes = 0;
cout << "Can you guess my number? ";
cin >> guess;
while (guess != 7) {
strikes++;
if (strikes >= 3) {
cout << "Sorry. The secret number is 7." << endl;
return 0;
}
if (guess > 7) cout << "Too big. ";
else cout << "Too small. ";
cout << "Try again:";
cin >> guess;
}
cout << "Congratulations." << endl;
return 0;
}

Problem 39
Write a complete C++ program that does the following. The user is given 5 chances to enter a
secret password which is 007. If the user fails the program says Goodbye, otherwise it says Hello. Here is a sample
run.
What is the password?
Wrong. Try again: 700
Wrong. Try again: 007
Hello.
Answer:

#include <iostream>
using namespace std;
int main() {
string guess;
int strikes = 0;
cout << "What is the password? ";
cin >> guess;
while (guess != "007") {
strikes++;
if (strikes >= 5) {
cout << "Goodbye." << endl;
return 0;
}
cout << "Wrong. Try again: ";
cin >> guess;
}
cout << "Hello." << endl;
return 0;
}
Problem 40
Write a complete C++ program that does the following. The user is given 5 chances to guess a
secret number, which is 7. Whenever the user makes an incorrect guess, the program announces whether the guess
was too big or too small. If the user succeeds, the program says Congratulations! otherwise it says Sorry. The
secret number is 7. Here is a sample run.
Can you guess my number?
Too big. Try again: 5
Too small. Try again: 7
Congratulations!

10

Answer:
#include <iostream>
using namespace std;
int main() {
int guess;
int strikes = 0;
cout << "Can you guess my number? ";
cin >> guess;
while (guess != 7) {
strikes++;
if (strikes >= 5) {
cout << "Sorry. The secret number is 7." << endl;
return 0;
}
if (guess > 7) cout << "Too big. ";
else cout << "Too small. ";
cout << "Try again:";
cin >> guess;
}
cout << "Congratulations." << endl;
return 0;
}
Problem 41
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.

int x, y;

(a) Print 12 copies of the word Hello on a single line of output.


Answer:
for (int c = 1; c <= 12; c++) cout << "Hello ";
cout << endl;

(b) Print the remainder when variable x is divided by variable y.


Answer:
cout << x % y << endl;

(c) Print the square root of 19. Use a C++ function for the calculation.
Answer:
cout << sqrt(19.0) << endl;

(d) Print a random number in the range 23 to 34, inclusive. Use a C++ function.
Answer:
cout << rand() % 12 + 23 << endl;

(e) Print the digits of the variable x backwards. So if x is 25, print 52.
Answer:
while (x > 0) {
cout << x % 10;
x = x / 10;
}
cout << endl;

Problem 42
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int a, b;

(a) Print the quotient when variable b is divided by variable a.


Answer:
cout << b / a << endl;

(b) Print b copies of the word Hi on a single line of output.


Answer:
for (int c = 1; c <= b; c++) cout << "Hi ";
cout << endl;

(c) Print a random number in the range 33 to 53, inclusive. Use a C++ function.
Answer:
cout << rand() % 21 + 33 << endl;

(d) Print the square root of 91. Use a C++ function for the calculation.
Answer:
cout << sqrt(91.0) << endl;

(e) Print the digits of a + b backwards. So if a + b is 25, print 52.


Answer:
a = a + b;
while (a > 0) {
cout << a % 10;
a = a / 10;
}
cout << endl;

Problem 43
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y;

(a) Print y copies of the word Hello on a single line of output.


Answer:
for (int c = 1; c <= y; c++) cout << "Hello ";
cout << endl;

(b) Print the remainder when variable x is divided by variable y.


Answer:
cout << x % y << endl;

(c) Print the square root of the square root of 19. Use a C++ function for the calculation.
Answer:

cout << sqrt(sqrt(19.0)) << endl;

(d) Print a random number in the range 123 to 126, inclusive. Use a C++ function.
Answer:
cout << rand() % 4 + 123 << endl;

(e) Print the digits of the variable y backwards. So if y is 25, print 52.
Answer:
while (y > 0) {
cout << y % 10;
y = y / 10;
}
cout << endl;

Problem 44
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int a, b;

(a) Print the exact quotient when variable b is divided by the sum of a and b.
Answer:
cout << ((double) b) / (a + b) << endl;

(b) Print b copies of the word Hi on a single line of output.


Answer:
for (int c = 1; c <= b; c++) cout << "Hi ";
cout << endl;

(c) Print a random number in the range 33 to 153, inclusive. Use a C++ function.
Answer:
cout << rand() % 121 + 33 << endl;

(d) Print the square root of the cube of 91. Use a C++ function for the calculation.
Answer:
cout << sqrt(91.0 * 91 * 91) << endl;

(e) Print the digits of a * b backwards. So if a * b is 25, print 52.


Answer:
a = a * b;
while (a > 0) {
cout << a % 10;
a = a / 10;
}
cout << endl;

Problem 45
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y, z;

(a) Print x copies of the number y on a single line of output.


Answer:
for (int c = 1; c <= x; c++) cout << y << " ";
cout << endl;

(b) Print the exact average of x, y and z.


Answer:
cout << (x + y + z) / 3.0 << endl;

(c) Print the square root of 5. Use a C++ function for the calculation.
Answer:
cout << sqrt(5.0) << endl;

(d) Print a random number in the range 1 to 6, inclusive. Use a C++ function.
Answer:
cout << rand() % 6 + 1 << endl;

(e) Print the first digit of the variable x.


Answer:
while (x >= 10) {
x = x / 10;
}
cout << x << endl;

Problem 46
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y, z;

(a) Print the bigger of x and y.


Answer:
if (x > y) cout << x << endl;
else cout << y << endl;

(b) Print x copies of the exact average of y and z on a single line of output.
Answer:
for (int c = 1; c <= x; c++) cout << (y + z) / 2.0 << " ";
cout << endl;

(c) Print a random number in the range 11 to 16, inclusive. Use a C++ function.
Answer:
cout << rand() % 6 + 11 << endl;

(d) Print the square root of 8. Use a C++ function for the calculation.
Answer:
cout << sqrt(8.0) << endl;

(e) Print the first digit of the sum x + y.


Answer:
x = x + y;
while (x >= 10) {
x = x / 10;
}
cout << x << endl;

Problem 47
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y, z;

(a) Print y copies of the number x on a single line of output.


Answer:

for (int c = 1; c <= y; c++) cout << x << " ";


cout << endl;

(b) Print the exact average of y and z.


Answer:
cout << (y + z) / 2.0 << endl;

(c) Print the square root of 50. Use a C++ function for the calculation.
Answer:
cout << sqrt(50.0) << endl;

(d) Print a random number in the range 3 to 8, inclusive. Use a C++ function.
Answer:
cout << rand() % 6 + 3 << endl;

(e) Print the first digit of the variable z.


Answer:
while (z >= 10) {
z = z / 10;
}
cout << z << endl;

Problem 48
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Assume the following variables have been declared and initialized with positive values.
int x, y, z;

(a) Print the smaller of x and y.


Answer:
if (x < y) cout << x << endl;
else cout << y << endl;

(b) Print z copies of the exact average of x and y on a single line of output.
Answer:
for (int c = 1; c <= z; c++) cout << (x + y) / 2.0 << " ";
cout << endl;

(c) Print a random number in the range 13 to 18, inclusive. Use a C++ function.
Answer:
cout << rand() % 6 + 13 << endl;

(d) Print the square root of 80. Use a C++ function for the calculation.
Answer:
cout << sqrt(80.0) << endl;

(e) Print the first digit of the sum y + z.


Answer:
x = y + z;
while (x >= 10) {
x = x / 10;
}
cout << x << endl;

Problem 49

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 4, y = 11, z = 91;
string freddy = "Fred";
string fred = "Freddy";
cout << "fred" << endl;
cout << z / y << endl;
if ((y > x) && (y > z)) cout << fred << endl;
cout << fred << freddy << endl;
cout << x << "x" << fred << "=" << z << "\n";
}
(a) What is the output at line (a)?
Answer:
fred
(b) What is the output at line (b)?
Answer:
8
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

FreddyFred
(e) What is the output at line (e)?
Answer:
4xFreddy=91

Problem 50

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 12, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Fred";
cout << z % y << endl;
cout << fred << endl;
cout << "fred" << freddy << endl;
if ((x > y) && (y > z)) cout << freddy << endl;
cout << x << y << "+" << z << "=" << x*y+z << "\n";
}
(a) What is the output at line (a)?
Answer:
9
(b) What is the output at line (b)?
Answer:
Fred
(c) What is the output at line (c)?
Answer:
fredFreddy
(d) What is the output at line (d)?
Answer:
Freddy
(e) What is the output at line (e)?
Answer:
1211+9=141

Problem 51

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 4, y = 11, z = 9;

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

string freddy = "Fred";


string fred = "Freddy";
cout << "freddy" << endl;
cout << (double) (z / x) << endl;
if ((y > x) && (y > z)) cout << fred << endl;
cout << fred << fred << endl;
cout << x << "x" << fred << "=" << z << "\n";

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

}
(a) What is the output at line (a)?
Answer:
freddy
(b) What is the output at line (b)?
Answer:
2
(c) What is the output at line (c)?
Answer:
Freddy
(d) What is the output at line (d)?
Answer:
FreddyFreddy
(e) What is the output at line (e)?
Answer:
4xFreddy=9

Problem 52

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 12, y = 11, z = 19;
string freddy = "Freddy";
string fred = "Fred";
cout << (double) (z % y) << endl;
cout << fred << endl;
cout << "freddy" << freddy << endl;
if ((x > y) && (y > z)) cout << freddy << endl;
cout << x << y << "+" << z << "=" << x*y+z << "\n";
}
(a) What is the output at line (a)?
Answer:
8
(b) What is the output at line (b)?
Answer:

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

Fred
(c) What is the output at line (c)?
Answer:
freddyFreddy
(d) What is the output at line (d)?
Answer:

(e) What is the output at line (e)?


Answer:
1211+19=151

Problem 53

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 14, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Fred";
cout << "fred" << endl;
cout << z / y << endl;
if ((y > x) && (y > z)) cout << fred << endl;
cout << fred << freddy << endl;
cout << x << "x" << fred << "=" << z << "\n";
}
(a) What is the output at line (a)?
Answer:
fred
(b) What is the output at line (b)?
Answer:
0
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:
FredFreddy
(e) What is the output at line (e)?
Answer:

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

14xFred=9
Problem 54

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 12, y = 11, z = 19;
string freddy = "Fred";
string fred = "Freddy";
cout << z % y << endl;
cout << fred << endl;
cout << "fred" << freddy << endl;
if ((x > y) && (y > z)) cout << freddy << endl;
cout << x << y << "+" << z << "=" << x*y+z << "\n";
}

//
//
//
//
//

line
line
line
line
line

(a) What is the output at line (a)?


Answer:
8
(b) What is the output at line (b)?
Answer:
Freddy
(c) What is the output at line (c)?
Answer:
fredFred
(d) What is the output at line (d)?
Answer:

(e) What is the output at line (e)?


Answer:
1211+19=151
Problem 55

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 14, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Freddy";
cout << "freddy" << endl;
cout << (double) (z / x) << endl;
if ((y > x) && (y > z)) cout << fred << endl;
cout << fred << fred << endl;
cout << x << "x" << fred << "=" << z << "\n";
}

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

(a)
(b)
(c)
(d)
(e)

(a) What is the output at line (a)?


Answer:
freddy
(b) What is the output at line (b)?
Answer:
0
(c) What is the output at line (c)?
Answer:

(d) What is the output at line (d)?


Answer:
FreddyFreddy
(e) What is the output at line (e)?
Answer:
14xFreddy=9

Problem 56

Consider the following C++ program.

#include <iostream>
using namespace std;
int main() {
int x = 19, y = 12, z = 11;
string freddy = "Fred";
string fred = "Fred";
cout << (double) (z % y) << endl;
cout << fred << endl;
cout << "freddy" << freddy << endl;
if ((x > y) && (y > z)) cout << freddy << endl;
cout << x << y << "+" << z << "=" << x*y+z << "\n";
}
(a) What is the output at line (a)?
Answer:
11
(b) What is the output at line (b)?
Answer:
Fred
(c) What is the output at line (c)?
Answer:
freddyFred

//
//
//
//
//

line
line
line
line
line

(a)
(b)
(c)
(d)
(e)

(d) What is the output at line (d)?


Answer:
Fred
(e) What is the output at line (e)?
Answer:
1912+11=239

Problem 57
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer between 1 and 20.
(2) If the user enters an illegal number, the program repeatedly asks the user to correct their number.
(3) If the user has not entered a correct number after 10 attempts, the program chooses 10 as the users number.
(4) The program prints the cube of the users number.
An example run of the program follows.
Enter an integer between 1 and 20: 100
Out of range. Enter an integer between 1 and 20: -1
Out of range. Enter an integer between 1 and 20: 5
The cube of your number is 125.
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer between 1 and 20: ";
cin >> n;
int strikes = 0;
while ( (n < 1) || (n > 20)) {
strikes++;
if (strikes >= 10) n = 10;
else {
cout << "Out of range. Enter an integer between 1 and 20: ";
cin >> n;
}
}
cout << "The cube of your number is " << n * n * n << endl;
return 0;
}

Problem 58
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer between 100 and 200.
(2) If the user enters an illegal number, the program repeatedly asks the user to correct their number.
(3) If the user has not entered a correct number after 3 attempts, the program exits.
(4) The program repeatedly generates and prints random numbers between 1 and 1000 until it generates the users
number when it stops.
An example run of the program follows.

Enter an integer between 100 and 200: 100


Random numbers: 7 873 924 428 100
Answer:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int n;
cout << "Enter an integer between 100 and 200: ";
cin >> n;
int strikes = 0;
while ( (n < 100) || (n > 200)) {
strikes++;
if (strikes >= 3) return 0;
cout << "Out of range. Enter an integer between 100 and 200: ";
cin >> n;
}
int random = 1000;
while (n != random) {
random = rand() % 1000 + 1;
cout << random << " ";
}
cout << endl;
return 0;
}

Problem 59
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer between 10 and 100.
(2) If the user enters an illegal number, the program repeatedly asks the user to correct their number.
(3) If the user has not entered a correct number after 5 attempts, the program chooses 10 as the users number.
(4) The program prints the square root of the users number.
An example run of the program follows.
Enter an integer between 10 and 100: 1
Out of range. Enter an integer between 10 and 100: 25
The square root of your number is 5.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter an integer between 10 and 100: ";
cin >> n;
int strikes = 0;
while ( (n < 10) || (n > 100)) {
strikes++;
if (strikes >= 5) n = 10;
else {
cout << "Out of range. Enter an integer between 10 and 100: ";

cin >> n;
}
}
cout << "The square root of your number is " << sqrt(n) << endl;
return 0;
}

Problem 60
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer between 50 and 100.
(2) If the user enters an illegal number, the program repeatedly asks the user to correct their number.
(3) If the user has not entered a correct number after 4 attempts, the program exits.
(4) The program repeatedly generates and prints random numbers between 1 and 1000 until it generates an exact
divisor of the users number when it stops.
An example run of the program follows.
Enter an integer between 50 and 100: 60
Random numbers: 7 873 924 428 100 30
Answer:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int n;
cout << "Enter an integer between 50 and 100: ";
cin >> n;
int strikes = 0;
while ( (n < 50) || (n > 100)) {
strikes++;
if (strikes >= 4) return 0;
cout << "Out of range. Enter an integer between 50 and 100: ";
cin >> n;
}
int random = 1000;
while (n % random != 0) {
random = rand() % 1000 + 1;
cout << random << " ";
}
cout << endl;
return 0;
}

Problem 61
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 1 and 20.
(2) If either of the users numbers is illegal the program asks the user to reenter both numbers. This continues until
two legal numbers have been entered.
(3) The program prints an a a square of Xs and an adjacent b b square of Ys. The squares should use top edges
that lie in the same line.
An example run of the program follows.

Enter two integers between 1 and 20: 5 3


XXXXXYYY
XXXXXYYY
XXXXXYYY
XXXXX
XXXXX
Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 1 and 20: ";
cin >> a >> b;
while ( (a < 1) || (a > 20) || (b < 1) || ( b > 20)) {
cout << "Out of range. Enter two integers between 1 and 20: ";
cin >> a >> b;
}
int max = a;
if (b > a) max = b;
for (int r = 1; r <= max; r++) {
for (int c = 1; c <= (a + b); c++) {
if (c <= a) {
if (r <= a) cout << "X";
else cout << " ";
}
if ((c > a) && (r <= b)) cout << "Y";
}
cout << endl;
}
return 0;
}

Problem 62
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 4 and 14.
(2) If either of the users numbers is illegal the program terminates.
(3) The program prints an a a square of Xs right on top of a b b square of Ys. The squares should line up so that
their left edges lie on the same line.
An example run of the program follows.
Enter two integers between 1 and 20: 5 4
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
YYYY
YYYY
YYYY
YYYY
Answer:

#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 4 and 14: ";
cin >> a >> b;
if ((a < 4) || (a > 14) || (b < 4) || ( b > 14))
return 0;
int max = a;
if (b > a) max = b;
for (int r = 1; r <= (a + b); r++) {
for (int c = 1; c <= max; c++) {
if (r <= a) {
if (c <= a) cout << "X";
else cout << " ";
}
if ((r > a) && (c <= b)) cout << "Y";
}
cout << endl;
}
return 0;
}

Problem 63
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 1 and 10.
(2) If either of the users numbers is illegal the program asks the user to reenter both numbers. This continues until
two legal numbers have been entered.
(3) The program prints an a a square of Xs and an adjacent b b square of Ys. The squares should use bottom
edges that lie in the same line.
An example run of the program follows.
Enter two integers between 1 and 10: 5 3
XXXXX
XXXXX
XXXXXYYY
XXXXXYYY
XXXXXYYY
Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 1 and 10: ";
cin >> a >> b;
while ( (a < 1) || (a > 10) || (b < 1) || ( b > 10)) {
cout << "Out of range. Enter two integers between 1 and 10: ";
cin >> a >> b;
}
int max = a;
if (b > a) max = b;
for (int r = max; r >= 1; r--) {

for (int c = 1; c <= (a + b); c++) {


if (c <= a) {
if (r <= a) cout << "X";
else cout << " ";
}
if ((c > a) && (r <= b)) cout << "Y";
}
cout << endl;
}
return 0;
}

Problem 64
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 4 and 14.
(2) If either of the users numbers is illegal the program terminates.
(3) The program prints an a a square of Xs right on top of a b b square of Ys. The squares should be lined up
so that their right edges lie on the same line.
An example run of the program follows.
Enter two integers between 4 and 14: 5 4
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
YYYY
YYYY
YYYY
YYYY
Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 4 and 14: ";
cin >> a >> b;
if ((a < 4) || (a > 14) || (b < 4) || ( b > 14))
return 0;
int max = a;
if (b > a) max = b;
for (int r = 1; r <= (a + b); r++) {
for (int c = max; c >= 1; c--) {
if (r <= a) {
if (c <= a) cout << "X";
else cout << " ";
}
if ((r > a) && (c <= b)) cout << "Y";
}
cout << endl;
}
return 0;
}

Problem 65
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 1 and 20.
(2) If either of the users numbers is illegal the program asks the user to reenter both numbers. This continues until
two legal numbers have been entered.
(3) The program prints a rows each of which contains a columns of Xs, but each pair of rows is separated by b blank
lines.
An example run of the program follows.
Enter two integers between 1 and 20: 4 2
XXXX

XXXX

XXXX

XXXX

Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 1 and 20: ";
cin >> a >> b;
while ( (a < 1) || (a > 20) || (b < 1) || ( b > 20)) {
cout << "Out of range. Enter two integers between 1 and 20: ";
cin >> a >> b;
}
for (int r = 1; r <= a; r++) {
for (int c = 1; c <= a; c++) cout << "X";
for (int blank = 0; blank <= b; blank++) cout << endl;
}
return 0;
}
Problem 66
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 2 and 10.
(2) If either of the users numbers is illegal the program terminates at once.
(3) The program prints a + b rows each of which contains a columns of Xs, but each pair of columns is separated by
b blank columns.
An example run of the program follows.
Enter
X X
X X
X X
X X
X X

two integers between 2 and 10: 3 2


X
X
X
X
X

Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 2 and 10: ";
cin >> a >> b;
if( (a < 2) || (a > 10) || (b < 2) || ( b > 10)) return 0;
for (int r = 1; r <= (a + b); r++) {
for (int c = 1; c <= a; c++) {
cout << "X";
for (int blank = 1; blank <= b; blank++) cout << " ";
}
cout << endl;
}
return 0;
}
Problem 67
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 1 and 15.
(2) If either of the users numbers is illegal the program asks the user to reenter both numbers. This continues until
two legal numbers have been entered.
(3) The program prints a rows each of which contains a b columns of Xs, but after every b complete rows it inserts
an extra blank line.
An example run of the program follows.
Enter two integers between 1 and 15: 5 2
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 1 and 15: ";
cin >> a >> b;
while ( (a < 1) || (a > 15) || (b < 1) || ( b > 15)) {
cout << "Out of range. Enter two integers between 1 and 15: ";
cin >> a >> b;
}
for (int r = 1; r <= a; r++) {
for (int c = 1; c <= a * b; c++) cout << "X";
cout << endl;
if (r % b == 0) cout << endl;
}
return 0;
}

Problem 68
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter two integers a and b with values between 3 and 12.
(2) If either of the users numbers is illegal the program terminates at once.
(3) The program prints a + b rows each of which contains a b columns of Xs, but after each group of b complete
columns the program prints a | symbol.
An example run of the program follows.
Enter two integers between 3 and 12: 4 5
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
XXXXX|XXXXX|XXXXX|XXXXX|
Answer:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers between 3 and 12: ";
cin >> a >> b;
if( (a < 3) || (a > 12) || (b < 3) || ( b > 12)) return 0;
for (int r = 1; r <= (a + b); r++) {
for (int c = 1; c <= a * b; c++) {
cout << "X";
if (c % b == 0) cout << "|";
}
cout << endl;
}
return 0;
}

Problem 69
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers less than n that are multiples of 5 (one number per line).
For example if the user enters 23 for n the program would output
5
10
15
20
Answer:
#include <iostream>
using namespace std;
int main() {

int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
for (int i = 1; i < n; i++)
if (i % 5 == 0) cout << i << endl;
return 0;
}

Problem 70
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
pi is 22/7
Answer:
cout << "pi is 22/7" << endl;
(ii) Print to the screen the value of x + y + z.
Answer:
cout << x + y + z << endl;
(iii) Read in the values of x, y, and z (in this order).
Answer:
cout << "Enter x,y and z: ";
cin >> x >> y >> z;
(iv) If the value of firstName is Freddy, print the message Goodbye. Otherwise print Hello.
Answer:
if (firstName == "Freddy") cout << "Goodbye" << endl;
else cout << "Hello" << endl;

(v) Prompt the user to enter a first name and last name and read their response to appropriate variables.
Answer:
cout << "Enter first name and last name:";
cin >> firstName >> lastName;

Problem 71
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;

cout << "Please enter two positive integers: ";


cin >> x >> y;
if (x <= 0) cout << "Illegal ";
if (y <= 0) {
cout << "Goodbye! " << endl;
return 0;
}
if ((x % 2 == 0) || (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) && (y > 10)) cout << -x << " ";
if (!(y > x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -5 4
Illegal -20
(b) The user enters: 4 -5
Goodbye!
(c) The user enters: 10 1
10 1
(d) The user enters: 1 10
10
(e) The user enters: 1 1
1

Problem 72
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints a triangular pattern with side n. Its horizontal side is at the top and its vertical side is at
the right.
For example, if the user enters 4 for n the program should print the following picture.
****
***
**
*
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
while (n <= 0) {

cout << "That is not positive. Try again: ";


cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
return 0;
}

Problem 73
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer n greater than 10.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers between n and 2n (inclusive) that are multiples of 5 (one number per
line).
For example if the user enters 13 for n the program would output
15
20
25
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer n greater than 10: ";
cin >> n;
if (n <= 10) return 0;
for (int i = n; i <= 2*n; i++)
if (i % 5 == 0) cout << i << endl;
return 0;
}

Problem 74
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
x is greater than y
Answer:
cout << "x is greater than y" << endl;
(ii) Print to the screen the value of xy + z.
Answer:

cout << x * y + z << endl;


(iii) Read in the values of z, y, and x (in this order).
Answer:
cout << "Enter z,y and x: ";
cin >> z >> y >> x;
(iv) If the value of firstName is Freddy, print the message No. Otherwise print the value of x.
Answer:
if (firstName == "Freddy") cout << "No" << endl;
else cout << x << endl;
(v) Use the approximation 22/7 to set the value of pi.
Answer:
pi = 22.0 / 7;

Problem 75
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 3) cout << "Error ";
if (y <= 3) {
cout << "Aha! " << endl;
return 0;
}
if ((x % 2 != 0) && (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) || (y > 10)) cout << -x << " ";
if (!(y == x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -5 4
Error -20 5 4
(b) The user enters: 4 -5
Aha!
(c) The user enters: 10 1
Aha!
(d) The user enters: 1 10
Error 10 10
(e) The user enters: 10 10

Problem 76
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program should terminate
immediately.
3. The program prints two copies of a triangular pattern with side n. Each triangle has a horizontal side at the top
and a vertical side at the right. The second copy is underneath the first.
For example, if the user enters 3 for n the program should print the following picture.
***
**
*
***
**
*
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 0) return 0;
for (int copy = 1; copy <= 2; copy++) {
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
}
return 0;
}

Problem 77
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer n that is between 20 and 40 (inclusive).
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers less than or equal to n/5 (one number per line).
For example if the user enters 28 for n the program would output
1
2
3
4
5
Answer:
#include <iostream>
using namespace std;
int main() {

int n;
cout << "Enter an integer n between 20 and 40: ";
cin >> n;
if (n < 20 || n > 40) return 0;
for (int i = 1; i <= n/5; i++)
cout << i << endl;
return 0;
}

Problem 78
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
Your firstName is x
Answer:
cout << "Your firstName is x" << endl;
(ii) Print to the screen the remainder when x + y is divided by z.
Answer:
cout << (x + y) % z << endl;
(iii) Read in the values of firstName and lastName (in this order).
Answer:
cout << "Enter first name and last name:";
cin >> firstName >> lastName;
(iv) If the value of firstName is not Freddy, print the message Hello. Otherwise end the program.
Answer:
if (firstName != "Freddy") cout << "Hello" << endl;
else return 0;
(v) Prompt the user to enter values for x, y and z and read their response to appropriate variables.
Answer:
cout << "Enter x,y and z: ";
cin >> x >> y >> z;

Problem 79
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;

if (x <= 0) cout << "Illegal ";


if (y <= 0) {
cout << "Goodbye! " << endl;
return 0;
}
if ((x % 2 == 0) || (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) && (y > 10)) cout << -x << " ";
if (!(y > x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -12 -12
Illegal Goodbye!
(b) The user enters: -12 12
Illegal -144 12
(c) The user enters: 1 12
12
(d) The user enters: 1 13

(e) The user enters: 12 12


144 12

Problem 80
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints a triangular pattern with side n. Its horizontal side is at the bottom and its vertical side is
at the right.
For example, if the user enters 4 for n the program should print the following picture.
*
**
***
****
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
while (n <= 0) {
cout << "That is not positive. Try again: ";
cin >> n;

}
for (int r = n; r >= 1; r--) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
return 0;
}

Problem 81
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n that is less than 40.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all even positive integers less than n (one number per line).
For example if the user enters 9 for n the program would output
2
4
6
8
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n less than 40: ";
cin >> n;
if (n <= 0 || n >= 40) return 0;
for (int i = 1; i < n; i++)
if (i % 2 == 0) cout << i << endl;
return 0;
}

Problem 82
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
x + y + z is positive
Answer:
cout << "x + y + z is positive" << endl;
(ii) Print to the screen the quotient of x + z by y. (Use integer division.)
Answer:

cout << (x + z) / y << endl;


(iii) Read in the values of x and pi (in this order).
Answer:
cout << "Enter x and pi: ";
cin >> x >> pi;
(iv) If the value of firstName is Freddy, print the value of pi. Otherwise print Hello.
Answer:
if (firstName == "Freddy") cout << pi << endl;
else cout << "Hello" << endl;
(v) If x is positive and z is negative print the value of y.
Answer:
if (x > 0 && z < 0) cout << y << endl;

Problem 83
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 3) cout << "Error ";
if (y <= 3) {
cout << "Aha! " << endl;
return 0;
}
if ((x % 2 != 0) && (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) || (y > 10)) cout << -x << " ";
if (!(y == x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -6 5
Error 6 5
(b) The user enters: 5 -6
Aha!
(c) The user enters: 11 2
Aha!
(d) The user enters: 2 11
Error -2 11
(e) The user enters: 11 11
-11

Problem 84
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program should terminate
immediately.
3. The program prints two copies of a triangular pattern with side n. Each triangle has a horizontal side at the
bottom and a vertical side at the right. The second copy is underneath the first.
For example, if the user enters 3 for n the program should print the following picture.
*
**
***
*
**
***
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 0) return 0;
for (int copy = 1; copy <= 2; copy++) {
for (int r = n; r >= 1; r--) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
}
return 0;
}

Problem 85
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers less than n that leave a remainder of 2 when they are divided by 5 (one
number per line).
For example if the user enters 23 for n the program would output
2
7
12
17
22
Answer:

#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
for (int i = 1; i < n; i++)
if (i % 5 == 2) cout << i << endl;
return 0;
}

Problem 86
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
PI is 22/7
Answer:
cout << "PI is 22/7" << endl;
(ii) Print to the screen the value of x/y + z (use integer division).
Answer:
cout << x / y + z << endl;
(iii) Read in the values of x and y (in this order).
Answer:
cout << "Enter x and y: ";
cin >> x >> y;
(iv) If the value of firstName is Freddy, exit the program. Otherwise print Hello.
Answer:
if (firstName == "Freddy") return 0;
else cout << "Hello" << endl;
(v) Prompt the user to enter a last name and first name and read their response to appropriate variables.
Answer:
cout << "Enter last name and first name:";
cin >> lastName >> firstName;

Problem 87
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {

int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) cout << "Illegal ";
if (y <= 0) {
cout << "Goodbye! " << endl;
return 0;
}
if ((x % 2 == 0) || (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) && (y > 10)) cout << -x << " ";
if (!(y > x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -4 5
Illegal -20
(b) The user enters: 5 -4
Goodbye!
(c) The user enters: 11 2
22 2
(d) The user enters: 2 11
22
(e) The user enters: 2 2
4 2

Problem 88
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints two adjacent triangular patterns with side n. Each triangle has a horizontal side at the top
and a vertical side at the right.
For example, if the user enters 4 for n the program should print the following picture.
**** ****
*** ***
**
**
*
*
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;

while (n <= 0) {
cout << "That is not positive. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int copy = 1; copy <= 2; copy++) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << " ";
}
cout << endl;
}
return 0;
}

Problem 89
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer n greater than 10.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers between n and 2n (inclusive) that leave a remainder of 1 when divided
by 5 (one number per line).
For example if the user enters 13 for n the program would output
16
21
26
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer n greater than 10: ";
cin >> n;
if (n <= 10) return 0;
for (int i = n; i <= 2*n; i++)
if (i % 5 == 1) cout << i << endl;
return 0;
}

Problem 90
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
x + y = 7
Answer:

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


(ii) Print to the screen quotient when xy is divided by z. (Use integer division.)
Answer:
cout << x * y / z << endl;
(iii) Read in the values of z, y, and x (in this order).
Answer:
cout << "Enter z,y and x: ";
cin >> z >> y >> x;
(iv) Use the approximation 22/7 to set the value of pi.
Answer:
pi = 22 / 7.0;
(v) If the value of firstName is not Freddy, print the message No. Otherwise print the value of x.
Answer:
if (firstName != "Freddy") cout << "No" << endl;
else cout << x << endl;

Problem 91
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 3) cout << "Error ";
if (y <= 3) {
cout << "Aha! " << endl;
return 0;
}
if ((x % 2 != 0) && (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) || (y > 10)) cout << -x << " ";
if (!(y == x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: 4 -5
Aha!
(b) The user enters: -5 4
Error -20 5 4
(c) The user enters: 10 10

(d) The user enters: 5 10

50 10
(e) The user enters: 10 5
5

Problem 92
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program should terminate
immediately.
3. The program prints two copies of a triangular pattern with side n. Each triangle has a horizontal side at the top
and a vertical side at the right. The second copy should be underneath and to the right of the first.
For example, if the user enters 3 for n the program should print the following picture.
***
**
*
***
**
*
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 0) return 0;
for (int copy = 1; copy <= 2; copy++) {
for (int r = 1; r <= n; r++) {
for (int skip = 1; skip <= (copy - 1) *n; skip++)
cout << " ";
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
}
return 0;
}

Problem 93
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter an integer n that is between 20 and 40 (inclusive).
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive odd integers that are less than or equal to n/5 (one number per line).
For example if the user enters 28 for n the program would output
1
3
5

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an
cin >> n;
if (n < 20 || n >
for (int i = 1; i
if (i % 2 == 1)
return 0;
}

integer n between 20 and 40: ";


40) return 0;
<= n/5; i++)
cout << i << endl;

Problem 94
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
Your name is x
Answer:
cout << "Your name is x" << endl;
(ii) Print to the screen the quotient when x is divided by yz (use integer division).
Answer:
cout << x / (y * z) << endl;
(iii) Read in the values of firstName and lastName (in this order).
Answer:
cout << "Enter first name and last name:";
cin >> firstName >> lastName;
(iv) If the value of x is 5 or firstName is not Freddy, print the message Hello. Otherwise end the program.
Answer:
if ((x == 5) || (firstName != "Freddy")) cout << "Hello" << endl;
else return 0;
(v) Prompt the user to enter values for x, y and z and read their response for x only.
Answer:
cout << "Enter x,y and z: ";
cin >> x;

Problem 95
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?

#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) cout << "Illegal ";
if (y <= 0) {
cout << "Goodbye! " << endl;
return 0;
}
if ((x % 2 == 0) || (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) && (y > 10)) cout << -x << " ";
if (!(y > x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: -12 -12
Illegal Goodbye!
(b) The user enters: 12 -12
Goodbye!
(c) The user enters: 12 12
144 12
(d) The user enters: 1 12
12
(e) The user enters: 1 13

Problem 96
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints two adjacent triangular patterns with side n. Each triangle has a horizontal side at the
bottom and a vertical side at the right.
For example, if the user enters 4 for n the program should print the following picture.
*
*
**
**
*** ***
**** ****
Answer:
#include <iostream>
using namespace std;
int main() {

int n;
cout << "Enter a positive integer: ";
cin >> n;
while (n <= 0) {
cout << "That is not positive. Try again: ";
cin >> n;
}
for (int r = n; r >= 1; r--) {
for (int copy = 1; copy <= 2; copy++) {
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << " ";
}
cout << endl;
}
return 0;
}

Problem 97
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n that is less than 40.
(2) If the user enters an illegal integer for n, the program terminates.
(3) The program prints all positive integers that square to a number less than n (one number per line).
For example if the user enters 9 for n the program would output
1
2
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n less than 40: ";
cin >> n;
if (n <= 0 || n >= 40) return 0;
for (int i = 1; i*i < n; i++)
cout << i << endl;
return 0;
}

Problem 98
Write C++ statements to carry out the following tasks. Do not write complete programs, just give
a single line, or a few lines of C++ instructions. Assume that the following declarations have been made and the
declared variables have been set to have legal values.
string firstName, lastName;
int x, y, z;
double pi;
(i) Print to the screen the message:
x % y + z is positive

Answer:
cout << "x % y + z is positive" << endl;
(ii) Print to the screen the quotient of x + z by x + y (use integer division).
Answer:
cout << (x + z) / (x + y) << endl;
(iii) Read in the values of pi and y (in this order).
Answer:
cout << "Enter pi and y: ";
cin >> pi >> y;
(iv) If the value of firstName is Freddy and y is equal to z, print the value of pi. Otherwise print No.
Answer:
if ((firstName == "Freddy") && (y == z)) cout << pi << endl;
else cout << "No" << endl;
(v) If x is positive and z is not even print the value of y.
Answer:
if ((x > 0) && (z % 2 != 0)) cout << y;

Problem 99
Consider the following C++ program. What is the exact output from the program in response to
each of the following user inputs?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 3) cout << "Error ";
if (y <= 3) {
cout << "Aha! " << endl;
return 0;
}
if ((x % 2 != 0) && (y % 2 == 0)) cout << x * y << " ";
if ((x <= 0) || (y > 10)) cout << -x << " ";
if (!(y == x)) cout << y;
cout << endl;
return 0;
}
(a) The user enters: 5 -6
Aha!
(b) The user enters: -6 5
Error 6 5
(c) The user enters: 11 11
-11

(d) The user enters: 3 11


Error -3 11
(e) The user enters: 11 5
5

Problem 100
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. The program asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program should terminate
immediately.
3. The program prints two copies of a triangular pattern with side n. Each triangle has a horizontal side at the
bottom and a vertical side at the right. The second copy should be underneath and to the right of the first.
For example, if the user enters 3 for n the program should print the following picture.
*
**
***
*
**
***
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 0) return 0;
for (int copy = 1; copy <= 2; copy++) {
for (int r = n; r >= 1; r--) {
for (int skip = 1; skip <= (copy - 1) *n; skip++)
cout << " ";
for (int c = 1; c <= n; c++) {
if (c >= r) cout << "*";
else cout << " ";
}
cout << endl;
}
}
return 0;
}

Problem 101
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a triangle with n rows using the number r to make the characters on row number r.
For example, if the user enters 4 for n the output is as follows:

1
22
333
4444
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++) cout << r;
cout << endl;
}
return 0;
}

Problem 102
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
2 + 2 = 3
Answer:
cout << "2 + 2 = 3";
(ii) Print the square root of 10.
Answer:
cout << sqrt(10.0);
(iii) Print a random number r with 7 r 27. (An appropriate C++ function must be used to make the random
number.)
Answer:
cout << rand() % 21 + 7;
(iv) Ask the user to enter their age. If their answer does not satisfy 5 age 99 exit the program immediately.
Answer:
cout << "Enter your age: ";
int age;
cin >> age;
if (age < 5 || age > 99) return 0;
(v) Print to the screen every two digit number n that is an exact multiple of 3. Print one number per line. (For
example 15 would be printed but 14 would not be printed since 15 = 5 3.)
Answer:
for (int n = 10; n <= 99; n++)
if (n % 3 == 0) cout << n << endl;

Problem 103
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (y <= 0)
cout << "Are you positive?\n";
while (y < 10) {
cout << y;
y = y + x;
}
cout << y << endl;
return 0;
}
(a) The user enters: -5 5
Illegal
(b) The user enters: 5 -5
Are you positive?
-50510
(c) The user enters: 10 1
111
(d) The user enters: 1 10
10
(e) The user enters: 1 1
12345678910

Problem 104
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer less than 1000.
2. If the entered number is out of range, the message Wrong! is printed and the program terminates.
3. Otherwise the program prints the product of the digits in the number that was entered.
Here is an example of how the program should work:
Enter a positive integer less than 1000:
Product of digits: 72
Answer:
#include <iostream>
using namespace std;

89

int main() {
int n;
cout << "Enter a positive integer less than 1000: ";
cin >> n;
if (n <= 0 || n >= 1000) {
cout << "Wrong!" << endl;
return 0;
}
int ans = 1;
while (n > 0) {
ans = ans * (n % 10);
n = n / 10;
}
cout << "Product of digits: " << ans << endl;
return 0;
}

Problem 105
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) Until the user enters a positive integer for n, the program makes the user enter another choice for n.
(3) The program prints a triangle with n rows, where the rows are formed by using the characters X and O in
sequence.
For example, if the user enters 4 for n the output is as follows:
X
OO
XXX
OOOO
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
while (n <= 0) {
cout << "Not positive. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++)
if (r % 2 == 1) cout << "X";
else cout << "O";
cout << endl;
}
return 0;
}

Problem 106
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:

1 x 2 x 3 x 4 = 24
Answer:
cout << "1 x 2 x 3 x 4 = 24";
(ii) Print a random number r with 11 r 29. (An appropriate C++ function must be used to make the random
number.)
Answer:
cout << rand() % 19 + 11;
(iii) Print the sum of the square roots of 11 and 12.
Answer:
cout << sqrt(11.0) + sqrt(12.0);
(iv) Ask the user to enter their age. If their answer does not satisfy 0 age 1000 exit the program immediately.
Answer:
cout << "Enter your age: ";
int age;
cin >> age;
if (age < 0 || age > 1000) return 0;
(v) Print to the screen every four digit number n that is divisible by both 6 and 10. Print one number per line. (For
example 6000 would be printed but 5999 would not be printed since 6000 = 6 1000 = 10 600.)
Answer:
for (int i = 1000; i <= 9999; i++)
if (i % 6 == 0 && i % 10 == 0) cout << i << endl;

Problem 107
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (y <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 0)
cout << "Are you positive?\n";
while (x < 10) {
cout << x;
x = y + x;
}
cout << y << endl;
return 0;
}
(a) The user enters: -5 5
Are you positive?
-5055

(b) The user enters: 5 -5


Illegal
(c) The user enters: 10 1
1
(d) The user enters: 1 10
110
(e) The user enters: 1 1
1234567891
Problem 108
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer less than 5000.
2. If the entered number is out of range, the message Wrong! is printed and the program terminates.
3. Otherwise the program prints the sum of the square roots of the digits in the number that was entered.
Here is an example of how the program should work:
Enter a positive integer less than 5000:
Sum of square roots of digits: 8

994

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer less than 5000: ";
cin >> n;
if (n <= 0 || n >= 5000) {
cout << "Wrong!" << endl;
return 0;
}
double ans = 0.0;
while (n > 0) {
ans = ans + sqrt((double) (n % 10));
n = n / 10;
}
cout << "Sum of square roots of digits: " << ans << endl;
return 0;
}
Problem 109
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a triangle with n rows using the number c to make the characters in column number c.
For example, if the user enters 4 for n the output is as follows:
1
12
123
1234

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++) cout << c;
cout << endl;
}
return 0;
}

Problem 110
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
Hello
Hello Hello
Answer:
cout << "Hello\nHello Hello\n";
(ii) Print the square root of 2.
Answer:
cout << sqrt(2.0);
(iii) Print a random number r with 7 r 0. (An appropriate C++ function must be used to make the random
number.)
Answer:
cout << rand() % 8 - 7;
(iv) Ask the user to enter their age. If their answer does not satisfy 5 age 99 print the word Illegal.
Answer:
cout << "Enter your age: ";
int age;
cin >> age;
if (age < 5 || age > 99) cout << "Illegal";
(v) Print to the screen every two digit number n that ends in the digit 4. Print one number per line. (For example
14 would be printed but 15 would not be printed.)
Answer:
for (int i = 10; i <= 99; i++)
if (i % 10 == 4) cout << i << endl;

Problem 111
Consider the following C++ program. What is the output from the program in response to the
following user input?

#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (y <= 0)
cout << "Are you positive?\n";
while (y < 20) {
cout << y;
y = y + 2 * x;
}
cout << y << endl;
return 0;
}
(a) The user enters: -5 5
Illegal
(b) The user enters: 5 -5
Are you positive?
-551525
(c) The user enters: 10 1
121
(d) The user enters: 1 10
101214161820
(e) The user enters: 1 1
13579111315171921

Problem 112
Write a complete C++ program that does the following.
1. It asks the user to enter an integer between 1000 and 9999.
2. If the entered number is out of range, the message Wrong! is printed and the program terminates.
3. Otherwise the program prints the two two digit number made from the first pair and last pair of digits (one
number per line).
Here is an example of how the program should work:
Enter an integer between 1000 and 9999:
45
67
Answer:
#include <iostream>
using namespace std;
int main() {

4567

int n;
cout << "Enter a positive integer between 1000 and 9999: ";
cin >> n;
if (n < 1000 || n > 9999) {
cout << "Wrong!" << endl;
return 0;
}
cout << n / 100 << endl << n % 100 << endl;
return 0;
}

Problem 113
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) Until the user enters a positive integer for n, the program makes the user enter another choice for n.
(3) The program prints a triangle with n rows, where the columns are formed by using the characters X and O in
sequence.
For example, if the user enters 4 for n the output is as follows:
X
XO
XOX
XOXO
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
while (n <= 0) {
cout << "Not positive. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= r; c++)
if (c % 2 == 1) cout << "X";
else cout << "O";
cout << endl;
}
return 0;
}

Problem 114
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
2 x 2 = 22
Answer:
cout << "2 x 2 = 22";

(iii) Print a random number r with 0 r 10. (An appropriate C++ function must be used to make the random
number.)
Answer:
cout << rand() % 11;
(ii) Print twice the square root of 17.
Answer:
cout << 2 * sqrt(17);
(iv) Ask the user to enter their age. If their answer does not satisfy 1 age 90 make them try exactly one more
time.
Answer:
cout << "Enter your age: ";
int age;
cin >> age;
if (age < 1 || age > 90) {
cout << "Illegal, try again: ";
cin >> age;
}
(v) Print to the screen every four digit number n that is a perfect square (of an integer). Print one number per line.
(For example 1600 would be printed but 1599 would not be printed since 1600 = 40 40.)
Answer:
for (int i = 1; i < 100; i++)
if (i * i >= 1000 && i * i <= 9999)
cout << i * i << endl;

Problem 115
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (y <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 0)
cout << "Are you positive?\n";
while (x < 20) {
cout << y;
x = x + 2 * y;
}
cout << x << endl;
return 0;
}
(a) The user enters: -5 5
Are you positive?
55525

(b) The user enters: 5 -5


Illegal
(c) The user enters: 10 1
1111120
(d) The user enters: 1 10
1021
(e) The user enters: 1 1
111111111121

Problem 116
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer less than 5000.
2. If the entered number is out of range, the message Wrong! is printed and the program terminates.
3. Otherwise the program prints the squares of the individual digits in the number number that was entered. (Pront
one square per line).
Here is an example of how the program should work:
Enter a positive integer less than 5000:
16
81
81

994

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer less than 5000: ";
cin >> n;
if (n <= 0 || n >= 5000) {
cout << "Wrong!" << endl;
return 0;
}
while (n > 0) {
cout << (n % 10) * ( n % 10) << endl;
n = n / 10;
}
return 0;
}

Problem 117
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a square with n rows and n columns using the letter X on or above the main diagonal and
the letter O below it.
For example, if the user enters 4 for n the output is as follows:

XXXX
OXXX
OOXX
OOOX
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n:";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if (c < r) cout << "O";
else cout << "X";
cout << endl;
}
return 0;
}

Problem 118
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
007 = 7
Answer:
cout << "007 = 7" << endl;
(ii) Print the numbers from 1 to 1000 to the screen, one number per line.
Answer:
for (int i = 1; i <= 1000; i++) cout << i << endl;
(iii) Print the numbers from 1 to 1000 to the screen, ten numbers per line.
Answer:
for (int i = 1; i <= 1000; i += 10) {
for (int j = i; j < i + 10; j++) cout << j << " ";
cout << endl;
}
(iv) Ask the user to enter their name, if they enter the name Freddy exit the program immediately.
Answer:
string name;
cout << "Enter your name: ";
cin >> name;
if (name == "Freddy") return 0;

(v) Print to the screen every two digit number n that is not an exact multiple of 3. Print one number per line. (For
example 14 would be printed but 15 would not be printed since 15 = 5 3.)
Answer:
for (int n = 10; n <= 99; n++)
if (n % 3 != 0)
cout << n << endl;

Problem 119
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (y <= 0) y = x;
if (x <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 10) cout << y << x << endl;
while (y > 0) {
cout << y;
y = y / 10;
}
cout << x << endl;
return 0;
}
(a) The user enters: -5 5
Illegal
(b) The user enters: 5 -5
55
55
(c) The user enters: -5 -5
Illegal
(d) The user enters: 567 123
123121567
(e) The user enters: 567 0
567565567

Problem 120
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer with 2 digits.
2. If the entered number is out of range, the message Too difficult! is printed and the program terminates.
3. Otherwise the program prints the sum of the two digits in the number that was entered.
Here is an example of how the program should work:

Enter a 2-digit integer:


Sum of digits: 17

89

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a 2 digit integer: ";
cin >> n;
if ( n < 10 || n > 99) {
cout << "Too difficult!" << endl;
return 0;
}
cout << "Sum of digits: " << n % 10 + n / 10 << endl;
return 0;
}

Problem 121
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a square with n rows and n columns using the letter A on or below the main diagonal and
the letter B above it.
For example, if the user enters 4 for n the output is as follows:
ABBB
AABB
AAAB
AAAA
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n:";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if (c <= r) cout << "A";
else cout << "B";
cout << endl;
}
return 0;
}

Problem 122
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
007 * 007 = 49
Answer:
cout << "007 * 007 = 49" << endl;
(ii) Print the even numbers from 2 to 400 to the screen, one number per line.
Answer:
for (int i = 2; i <= 400; i += 2) cout << i << endl;
(iii) Print the even numbers from 2 to 400 to the screen, ten numbers per line.
Answer:
for (int i = 2; i <= 400; i += 20) {
for (int j = i; j < i + 20; j += 2) cout << j << " ";
cout << endl;
}
(iv) Ask the user to enter their name, if they enter the name Freddy print Hello to the screen, otherwise print
nothing.
Answer:
string name;
cout << "Enter your name: ";
cin >> name;
if (name == "Freddy") cout << "Hello" << endl;
(v) Print to the screen every number n that is less than 100 and is either an exact multiple of 3 or an exact multiple
of 5. Print one number per line. (For example 14 would not be printed but 15 would be printed.)
Answer:
for (int n = 1; n <= 99; n++)
if (n % 3 == 0 || n % 5 == 0)
cout << n << endl;
Problem 123
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) x = y;
if (y <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 10) cout << y << x << endl;
while (y > 0) {
cout << y;
y = y / 10;
}
cout << x << endl;
return 0;
}

(a) The user enters: -5 5


55
55
(b) The user enters: 5 -5
Illegal
(c) The user enters: -5 -5
Illegal
(d) The user enters: 567 123
123121567
(e) The user enters: 567 0
Illegal
Problem 124
Write a complete C++ program that does the following.
1. It asks the user to enter two different positive integers each of which has 2 digits.
2. If the input is illegal, the message Too easy! is printed and the program terminates.
3. Otherwise the program prints the absolute value of the difference of the numbers that were entered.
Here is an example of how the program should work:
Enter two different 2-digit integers:
Absolute difference: 2

89 91

Answer:
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter two different 2 digit integers: ";
cin >> n >> m;
if ( n < 10 || n > 99 || m < 10 || m > 99 || n == m) {
cout << "Too easy!" << endl;
return 0;
}
cout << "Absolute difference: ";
if (n > m) cout << n - m << endl;
else cout << m - n << endl;
return 0;
}
Problem 125
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a square with n rows and n columns using the letter X on odd numbered rows and O on
even numbered rows.
For example, if the user enters 4 for n the output is as follows:

XXXX
OOOO
XXXX
OOOO
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n:";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if (r % 2 == 0) cout << "O";
else cout << "X";
cout << endl;
}
return 0;
}

Problem 126
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
007 = Bond
Answer:
cout << "007 = Bond" << endl;
(ii) Print the numbers from -100 to 100 to the screen, one number per line.
Answer:
for (int i = -100; i <= 100; i++) cout << i << endl;
(iii) Print the numbers from -100 to 100 to the screen, three numbers per line.
Answer:
for (int i = -100; i <= 100; i += 3) {
for (int j = i; j < i + 3; j++) cout << j << " ";
cout << endl;
}
(iv) Ask the user to enter their name, if they enter the name 007 exit the program immediately.
Answer:
string name;
cout << "Enter your name: ";
cin >> name;
if (name == "007") return 0;

(v) Print to the screen every four digit number n that is not an exact multiple of 7. Print one number per line. (For
example 1000 would be printed but 1001 would not be printed since 1001 = 7 143.)
Answer:
for (int n = 1000; n <= 9999; n++)
if (n % 7 != 0)
cout << n << endl;

Problem 127
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (y <= 0) y = x + 1;
if (x <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 10) cout << y << x << endl;
while (y > 100) {
cout << y;
y = y / 100;
}
cout << x << endl;
return 0;
}
(a) The user enters: -5 5
Illegal
(b) The user enters: 5 -5
65
5
(c) The user enters: -5 -5
Illegal
(d) The user enters: 567 123
123567
(e) The user enters: 567 0
568567

Problem 128
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer with 2 digits.
2. If the entered number is out of range, the message Too difficult! is printed and the program terminates.
3. Otherwise the program prints the absolute value of the difference of the two digits in the number that was entered.
Here is an example of how the program should work:

Enter a 2-digit integer:


Absolute difference: 2

79

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a 2 digit integer: ";
cin >> n;
if ( n < 10 || n > 99) {
cout << "Too difficult!" << endl;
return 0;
}
int ans = n % 10 - n / 10;
if (ans < 0) ans = -ans;
cout << "Absolute difference: " << ans << endl;
return 0;
}

Problem 129
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a square with n rows and n columns using the letter A on odd numbered columns and B on
even numbered columns.
For example, if the user enters 4 for n the output is as follows:
ABAB
ABAB
ABAB
ABAB
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n:";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++)
if (c % 2 == 0) cout << "B";
else cout << "A";
cout << endl;
}
return 0;
}

Problem 130
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print to the screen the message:
000 + 7 = 007
Answer:
cout << "000 + 7 = 007" << endl;
(ii) Print the even numbers from -200 to 200 to the screen, one number per line.
Answer:
for (int i = -200; i <= 200; i += 2) cout << i << endl;
(iii) Print the even numbers from -200 to 200 to the screen, three numbers per line.
Answer:
for (int i = -200; i <= 200; i += 6) {
for (int j = i; j < i + 6; j += 2) cout << j << " ";
cout << endl;
}
(iv) Ask the user to enter their name, if they enter the name 007 print the message James Bond otherwise print
their name.
Answer:
string name;
cout << "Enter your name: ";
cin >> name;
if (name == "007") cout << "James Bond" << endl;
else cout << name;
(v) Print to the screen every four digit number n that is an exact multiple of 7 and an exact multiple of 11. Print
one number per line. (For example 1000 would not be printed but 1001 would be printed since 1001 = 7 143 and
1001 = 11 91.)
Answer:
for (int n = 1000; n <= 9999; n++)
if (n % 7 == 0 && n % 11 == 0)
cout << n << endl;

Problem 131
Consider the following C++ program. What is the output from the program in response to the
following user input?
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Please enter two positive integers: ";
cin >> x >> y;
if (x <= 0) x = y + 1;
if (y <= 0) {
cout << "Illegal" << endl;
exit (1);
}
if (x <= 10) cout << y << x << endl;
while (y > 100) {

cout << y;
y = y / 100;
}
cout << x << endl;
return 0;
}
(a) The user enters: -5 5
56
6
(b) The user enters: 5 -5
Illegal
(c) The user enters: -5 -5
Illegal
(d) The user enters: 567 123
123567
(e) The user enters: 567 0
Illegal

Problem 132
Write a complete C++ program that does the following.
1. It asks the user to enter two different positive integers each of which has 2 digits.
2. If the input is illegal, the message Illegal! is printed and the program terminates.
3. Otherwise the program prints the larger of the numbers that were entered.
Here is an example of how the program should work:
Enter two different 2-digit integers:
Larger: 91

89 91

Answer:
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter two different 2 digit integers: ";
cin >> n >> m;
if ( n < 10 || n > 99 || m < 10 || m > 99 || n == m) {
cout << "Illegal!" << endl;
return 0;
}
cout << "Larger: ";
if (n > m) cout << n << endl;
else cout << m << endl;
return 0;
}

Problem 133
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a rectangle of symbols with n rows and twice as many columns as rows.
For example, if the user enters 4 for n the output is as follows:
********
********
********
********
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= 2 * n; c++) cout << "*";
cout << endl;
}
return 0;
}

Problem 134
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions. Assume that the following variables have been declared, and
if necessary have values, for each part:
int number;
double x, y;
Declare any other variables that you use.
(i) Print all integers from number down to 10. For example if number is 13, the output should be 13 12 11 10. (If
number < 10, nothing is printed.)
Answer:
while (number >= 10) {
cout << number << " ";
number--;
}
(ii) Print the value of the sum of squares of x and y.
Answer:
cout << x * x + y * y << endl;
(iii) Calculate x as the decimal that represents the fraction 5/7.
Answer:
x = 5.0 / 7.0;

(iv) Print the sum of the square roots of all the numbers from 1048576 to 5764801.
Answer:
double sum = 0;
for (int n = 1048576; n<= 5764801; n++)
sum += sqrt((double) n);
cout << sum << endl;
(v) Print every three digit number n for which the next to last digit of n2 is 2. For example, 111 is printed because
1112 = 12321. (This number ends in the digits 21 and its next to last digit is 2.)
Answer:
for (int n = 100; n <= 999; n++)
if ( (20 <= (n * n) % 100) && ((n * n) % 100 <= 29))
cout << n << endl;

Problem 135
Consider the following C++ program. What is the output from the program in response to the
following user inputs?
#include <iostream>
using namespace std;
int main () {
int n;
cout << "Please give me an integer: ";
cin >> n;
if (n < 10) {
cout << "Integer is too small." << endl;
if (n < 0) return 0;
}
if (n % 2 == 0) cout << 3 * n / 2 << endl;
else if (n % 4 == 1) cout << 3 * ((n - 1) / 4) + 1;
else cout << 3 * ((n + 1) / 4) - 1;
cout << endl;
return 0;
}
(a) The user enters: -9
Integer is too small.
(b) The user enters: 9
Integer is too small.
7
(c) The user enters: 10
15
(d) The user enters: 11
8
(e) The user enters: 21
16

Problem 136
Write a complete C++ program that does the following.
1. It repeatedly, asks the user to enter an integer.
2. If the entered number is negative, the word Negative is printed and the program terminates.
3. Otherwise the square root of the number is calculated and the nearest integer to this square root is printed.
Here is an example of how the program should work:
Enter an
10
Enter an
10
Enter an
10
Enter an
Negative

integer:
integer:

100
97

integer: 101
integer: -100

Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 0;
while (n >= 0) {
cout << "Enter an integer n: ";
cin >> n;
if (n < 0) {
cout << "Negative" << endl;
return 0;
}
cout << (int) (sqrt((double) n) + 0.5) << endl;
}
return 0;
}

Problem 137
Write a complete C++ program that does the following.
1. It asks the user to enter an integer.
2. If the entered number is even it divides the number by 2.
3. Otherwise the program multiplies the number by 3 and adds 1.
4. It prints the result.
Here is an example of how the program should work:
Enter an integer:
5
The answer is:
16
Answer:
#include <iostream>
using namespace std;
int main () {
int n;
cout << "Enter an integer: ";
cin >> n;

if (n % 2 == 0) n = n / 2;
else n = n * 3 + 1;
cout << "The answer is: " << n << endl;
return 0;
}

Problem 138
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions. Assume that the following variables have been declared, and
if necessary have values, for each part:
int number;
double x, y;
Declare any other variables that you use.
(i) Print all integers from 1 through number.
Answer:
for (int i = 1; i <= number; i++)
cout << i << " ";
(ii) Print the value of the larger of x and y.
Answer:
if (x < y) cout << y;
else cout << x;
(iii) Calculate x as the decimal that represents the fraction 1/7.
Answer:
x = 1.0 / 7;
(iv) Print the sum of the square roots of the numbers 19683, 19684, and 19685.
Answer:
double sum = 0;
for (int x = 19683; x <= 19685; x++)
sum += sqrt ((double) x);
cout << sum << endl;
(v) Print every three digit number n for which the square of n ends with the digits 21. For example, 111 is printed
because 1112 = 12321.
Answer:
for (int n = 100; n <= 999; n++)
if (n * n % 100 == 21) cout << n << endl;

Problem 139
Consider the following C++ program. What is the output from the program in response to the
following user inputs?
#include <iostream>
using namespace std;
int main () {
int n;
cout << "Please give me an integer: ";
cin >> n;

if (n < 10) {
cout << "Integer is too small." << endl;
if (n < 0) return 0;
}
if (n % 3 == 0) cout << "n = " << n << endl;
else cout << "CSCI 111" << endl;
while (n > 20) {
cout << n << ", ";
n = n - 10;
}
cout << endl;
return 0;
}
(a) The user enters: -1 Answer:
Integer is too small.
(b) The user enters: 0 Answer:
Integer is too small.
n = 0
(c) The user enters: 1 Answer:
Integer is too small.
CSCI 111
(d) The user enters: 19 Answer:
CSCI 111
(e) The user enters: 111 Answer:
n = 111
111, 101, 91, 81, 71, 61, 51, 41, 31, 21,

Problem 140
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
(1) The program asks the user to enter a positive integer n.
(2) If the user enters a non-positive integer for n, the program terminates.
(3) The program prints a triangle with n rows whose straight vertical edge is at the right of the picture.
For example, if the user enters 4 for n the output is as follows:
*
**
***
****
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;

for (int r = n; r >= 1; r--){


for (int c = 1; c <= n; c++)
if (c >= r) cout << "*";
else cout << " ";
cout << endl;
}
return 0;
}
Problem 141
Write a complete C++ program that does the following.
1. It asks the user to enter a decimal number that is greater than 0 and less than 10.
2. If the entered number is not within the desired range the program exits.
3. Otherwise the program prints the square of the number.
Here is an example of how the program should work:
Enter a number greater than 0 and less than 10:
The square is:
6.25

2.5

Answer:
#include <iostream>
using namespace std;
int main() {
double number;
cout << "Enter a number greater than 0 and less than 10:
cin >> number;
if (number <= 0 || number >= 10) exit(1);
cout << "The square is:
" << number * number << endl;
return 0;
}

";

Problem 142
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print to the screen the message:
2 + 2 = 5
cout << "2 + 2 = 5" << endl;
(b) Print all the odd numbers from 1 to 1000 to the screen (one number per line).
for (int i = 1; i<= 1000; i++)
if (i % 2 == 1) cout << i << endl;
(c) Ask the user enter a number that is not a multiple of 10. If the user gives an incorrect response force the user to
keep entering a number until legal answer is received.
cout << "Enter a number that is not a multiple of 10: ";
int number;
cin >> number;
while (number %10 == 0) {
cout << "Thats a multiple of 10. Try again: ";
cin >> number;
}

(d) Ask the user to enter a number and print its square root if it is positive. (Otherwise do not print anything.)
cout << "Enter a positive number: ";
double x;
cin >> x;
if (x > 0) cout << sqrt(x) << endl;
(e) Read an integer greater than 2 from the user, then print it in reverse. (If the user enters the number 125, the
program should print 521.)
int n;
cout << "Enter a number greater than 2: ";
cin >> n;
while (n > 0) {
cout << n % 10;
n = n / 10;
}
cout << endl;

Problem 143
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int age;
string name;
cout << "Please enter your name and age: ";
cin >> name >> age;
if (name == "Kamil") exit(0);
if (age < 0) {
name = "Kamil";
age = 5;
}
if (name == "Peter") {
cout << "You rat!" << endl;
return 0;
}
if (age >= 100) {
cout << "Goodbye Kamil!" << endl;
}
cout << " Hello " << name << " you are about "
return 0;
}
(i) The user enters: Freddy 17
Hello Freddy you are about 17
(ii) The user enters: Peter 19
You rat!
(iii) The user enters: Kamil 19

<< age << endl;

(iv) The user enters: Andrew -20


Hello Kamil you are about 5
(v) The user enters: Carl 200
Goodbye Kamil!
Hello Carl you are about 200

Problem 144
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
The program prints a table with 100 lines of output. On output line number x the program should list the first x
odd numbers.
For example, the first 4 lines of output read as follows:
1
1 3
1 3 5
1 3 5 7
Answer:
#include <iostream>
using namespace std;
int main() {
for (int r = 1; r <= 100; r++) {
for (int c = 1; c < 2*r; c += 2)
cout << c << " ";
cout << endl;
}
return 0;
}

Problem 145
Write a complete C++ program that asks a user to enter their day and month of birth. If the
users birthday is March 14th , the program wishes the user a Happy Birthday, otherwise it just says Hello. For
example, the program could run as follows:
What is your day and month of birth:
Happy Birthday.

14 March

#include <iostream>
using namespace std;
int main() {
int d;
string month;
cout << "Enter your day and month of birth: ";
cin >> d >> month;
if (d == 14 && month == "March")
cout << "Happy Birthday." << endl;
else cout << "Hello" << endl;
return 0;
}

Problem 146
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print to the screen the message (the word Hello repeats 10 times):

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
for (int c = 1; c <= 10; c++) cout << "Hello ";
(b) Read an integer from the user and print the integer without its last digit. (For example if the user enters 19683,
the program would print 1968.)
int n;
cin >> n;
cout << n / 10;
(c) Print the square root of 19683 to the output screen:
cout << sqrt(19683);
(d) Ask the user enter a name. If the user says Freddy, force the user to keep entering a name until something else
is received.
string name;
cout << "Who are you: ";
cin >> name;
while (name == "Freddy") {
cout << "No! Who are you: ";
cin >> name;
}
(e) Print a random number between 1000 and 9999 to the screen.
cout << rand() % 9000 + 1000;

Problem 147
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int n, m;
cout << "Please two integers: ";
cin >> m >> n;
if (n == 0 && m == 0) cout << n << endl;;
if (n == 0 || m == 0) exit(1);
if (n < 0 && m < 0) cout << " Negative" << endl;
else {
if (n < m) cout << n << endl;
}
if (m > 7) cout << " 7" << n << endl;
return 0;
}
(i) The user enters: 0 0
0
(ii) The user enters: 0 10

(iii) The user enters: -10 -10


Negative
(iv) The user enters: 10 -10
-10
7-10
(v) The user enters: 10 10
710

Problem 148
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an integer that is at least 3.
2. The program reads a value x entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of x has been entered.
3. The program prints a picture with x columns. The picture should display a left pointing arrow pattern.
For example, if the user enters 4 for x the program should print the following picture.
*
*
*
*
*
*
*
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter an integer that is at least 3: ";
cin >> x;
while (x < 3) {
cout << "Must be at least 3. Try again: ";
cin >> x;
}
int rows = 2 * x - 1;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= x; c++) {
if ((r + c == x + 1) || (r - c == x - 1))
cout << "*";
else cout << " ";
}
cout << endl;
}
return 0;
}

Problem 149

Write a complete C++ program that does the following.

1. It asks the user to enter their favorite positive integer.


2. The program prints the square root of that integer.
Here is an example of how the program should work:
Enter your favorite positive integer:
It has square root: 5.0

25

Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter your favorite positive integer: ";
cin >> n;
cout << "It has square root: " << sqrt(n) << endl;
return 0;
}

Problem 150
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print to the screen the message:
2 + 2 = 4
cout << "2 + 2 = 4";
(b) Print all the numbers from 1 to 1000 to the screen (one number per line).
for (int c = 1; c <= 1000; c++)
cout << c << endl;
(c) Ask the user enter a multiple of 3. If the user gives an incorrect response force the user to keep entering a number
until a multiple of 3 is received.
int n;
cout << "Enter a multiple of 3: ";
cin >> n;
while ( n % 3 != 0 ) {
cout << "Wrong. Enter a multiple of 3: ";
cin >> n;
}
(d) Print 10 random numbers each between 10 and 20 to the output screen:
for (int c = 1; c <= 10; c++) {
cout << rand() % 11 + 10 << endl;
}
(e) Read an integer greater than 2 from the user, then print its largest factor. (For this problem, a factor of x is a
number f with 1 f < x that divides into x without remainder.)

int n;
cout << "Enter an integer greater than 2: ";
cin >> n;
factor = n - 1;
while ( n % factor > 0) factor--;
cout << factor;

Problem 151
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int n; string name;
cout << "Please enter your name and an integer: ";
cin >> name >> n;
if (n == 0
if (n == 0
if (n < 0)
else {
cout <<
}
if (n > 7)
return 0;

&& name == "Freddy") cout << name << endl;;


|| name == "Freddy") exit(1);
cout << " Negative" << endl;
" name " << name << " name " << endl;
cout << " 7 " << endl;

}
(i) The user enters: Freddy 0
Freddy
(ii) The user enters: Freddy 10

(iii) The user enters: Fred -10


Negative
(iv) The user enters: Fred 5
name Fred name
(v) The user enters: Fred 10
name Fred name
7

Problem 152
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
The program prints a table with 100 lines of output. On output line number x the program should list the numbers
from x to x2 together with their sum.
For example, the first 4 lines of output read as follows:

1
2
3
4

the
3 4
4 5
5 6

sum
the
6 7
7 8

is 1
sum is 9
8 9 the sum is 42
9 10 11 12 13 14 15 16 the sum is 130

Answer:
#include <iostream>
using namespace std;
int main() {
for (int x = 1; x <= 100; x++) {
int sum = 0;
for (int c = x; c <= x * x; c++) {
cout << c << " ";
sum = sum + c;
}
cout << "the sum is " << sum << endl;
}
return 0;
}

Problem 153
Write a complete C++ program that does the following.
1. It asks the user to enter the number of quarters, dimes, nickels and cents that they are carrying.
2. The program then reports the total amount of change that the user has.
Here is an example of how the program should work:
How many quarters do you have?
How many dimes do you have?
How many nickels do you have?
How many cents do you have ?
That makes 216 cents in change.

7
2
3
6

Answer:

#include <iostream>
using namespace std;
int main () {
int q, d, n, p;
cout << "How many quarters do you have? ";
cin >> q;
cout << "How many dimes do you have? ";
cin >> d;
cout << "How many nickels do you have? ";
cin >> n;
cout << "How many pennies do you have? ";
cin >> p;
cout << "That makes "
<< ((q * 25) + (d * 10) + (n * 5) + p) << " in change.";
cout << endl;

return 0;
} //main

Problem 154
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print a random number between -1 and -9 to the output screen:
Answer:
int r = rand() % 9 + 1;
cout << -r;
(b) Print (to the output screen) the sum of the square roots of the numbers 1, 2, 3, 4, 5 and 6.
Answer:
double sum = 0;
for (int s = 1; s <= 6; s++)
sum += sqrt(s);
cout << sum;
(c) Ask the user to enter the word Hello. Force the user to keep entering a new word until an input equal to
Hello is received.
Answer:
string input = "";
while (input != "Hello") {
cout << "Please enter the word Hello: ";
cin >> input;
}

(d) Print twelve random negative numbers.


Answer:
for (int n = 1;
int r =
if (r >
cout <<
}

n <= 12; n++) {


rand();
0) r = -r;
r;

(e) Print the largest integer whose square root is less than 1729.
Answer:
int n = 1;
while (sqrt(n) < 1729) n++;
cout << n - 1;

Problem 155
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int n, m; string name;
cout << "Please enter two integers followed by your name: ";

cin >> m >> n >> name;


if(n == 0) exit(1);
if(m >= n) cout << name;
if(m % n == 1) cout << name << name;
else while (n > 7) {
cout << n;
n = n - m;
}
cout << endl;
return 0;
}
(i) The user enters: 2 2 Freddy
Answer:
Freddy
(ii) The user enters: 0 7 007
Answer:

(iii) The user enters: 1 10 X


Answer:
XX
(iv) The user enters: 1 2 3
Answer:
33
(v) The user enters: 11 111 Freddy
Answer:
1111008978675645342312

Problem 156
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an odd positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program terminates.
3. The program prints an n n grid displaying a large letter X. The left half of the X should be made with the
character +, the right half should be made with the character x and the very center should be a .
For example, if the user enters 7 for n the program should print the following picture.
+
+

x
x
+ x
*
+ x
+
x
+
x
Answer:

#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Please enter an odd positive integer ";
cin >> n;
if (n < 0 || n % 2 != 1) exit (1);
int middle = (n / 2) + 1;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
if (r == middle && c == middle)
cout << "*";
else if ((r == c || r + c == n + 1) && c < middle)
cout << "+";
else if ((r == c || r + c == n + 1) && c > middle)
cout << "x";
else
cout << " ";
} //for
cout << endl;
} //for
return 0;
} //main

Problem 157
Write a complete C++ program that does the following.
1. It asks the user to enter their age (which is assumed to be a positive integer).
2. The program should print the word Hello once for each year of the users age.
Here is an example of how the program should work:
Enter your age: 5
Hello Hello Hello Hello Hello
Answer:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age:
";
cin >> age;
for (int x = 1; x <= age; x++)
cout << "Hello" << " ";
cout << endl;
return 0;
}

Problem 158
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print a random number that has 3 digits to the output screen:
Answer:

cout << rand()%900 + 100 << endl;


(b) Print (to the output screen) the smallest integer whose square root is larger than 3141.5926:
Answer:
int k = (int) (3141.5926 * 3141.5926);
cout << k + 1 << endl;
(c) Ask the user to type a password and then to type it again. Print Error if the two words are different.
Answer:
string pw1, pw2;
cout << "Enter a password twice ";
cin >> pw1 >> pw2;
if (pw1 != pw2) cout << "Error" << endl;
(d) Read a positive integer greater than 2 from the user, and print its largest factor. (For this problem a number f
is a factor of the number x if 1 f x 1 and f divides into x without remainder.)
Answer:
cout << "Enter a postive integer (greater than 2): ";
cin >> n;
m = n - 1;
while (n % m != 0) m--;
cout << m << endl;
(e) Read a name from the user. If necessary, repeatedly ask the user to reenter a name until the user has said Freddy.
Answer:
string name;
cout << "You are Freddy. What is your name: ";
cin >> name;
while (name != "Freddy") {
cout << "Wrong! What is your name: ";
cin >> name;
}
Problem 159
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int n, m;
cout << "Please enter two integers: ";
cin >> n >> m;
if (n > m) cout << n % m << endl;
else {
for (int r = 1; r < n; r++) {
for (int c = 1; c < m - n - 1; c++) {
cout << "*+";
}
cout << endl;
if (n == 10) exit(1);
}
}
return 0;
}

(i) The user enters: 10 9


Answer:
1
(ii) The user enters: 3 7
Answer:
*+*+
*+*+
(iii) The user enters: 3 15
Answer:
*+*+*+*+*+*+*+*+*+*+
*+*+*+*+*+*+*+*+*+*+
(iv) The user enters: 10 15
Answer:
*+*+*+
(v) The user enters: -1 5
Answer:

Problem 160
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer.
2. If the input is illegal, the program should terminate.
3. The program prints the digits of the number in reverse order (separated by spaces) and then gives their sum.
For example, if the user enters 19683 the program should print the following output.
3 8 6 9 1 sum to 27
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 0) exit(1);
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
n = n / 10;
sum += digit;
cout << digit << " ";
}
cout << "sum to " << sum << endl;
return 0;
}

Problem 161
Write a complete C++ program that does the following.
1. It asks the user to enter their age (which is assumed to be a positive integer).
2. If the user is a teenager, the program should print Hello Teenager otherwise it should just print Hello.
Here is an example of how the program should work:
Enter your age:
Hello Teenager

15

Answer:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age:
";
cin >> age;
if ((13 <= age) && (age <= 19)) cout << "Hello Teenager" << endl;
else cout << "Hello" << endl;
return 0;
}

Problem 162
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print 5 random numbers each between 1 and 9 to the output screen:
Answer:
for (int i = 1; i <= 5; i++) cout << rand()%9 + 1 << endl;
(b) Print (to the output screen) the square root of 19683:
Answer:
cout << sqrt(19683) << endl;
(c) Ask the user enter a positive integer and if the user gives a non-positive response force the user to keep entering
a number until a positive input is received.
Answer:
cout << "Enter a positive integer: ";
cin >> n;
while (n <= 0) {
cout << "Wrong! try again: ";
cin >> n;
}
(d) Read an integer greater than 2 from the user, then print its smallest factor. (For this problem, a factor of x is a
number f with 2 f x that divides into x without remainder.)
Answer:
cout << "Enter a postive integer (greater than 2): ";
cin >> n;
int f = 2;
while (n % f != 0) f++;
cout << f << endl;
(e) Read a name from the user and exit the program if the name is Freddy.
Answer:

string name;
cout << "What is your name: ";
cin >> name;
if (name == "Freddy") exit(1);
Problem 163
Consider the following C++ program. Write exactly what output is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int main(){
int n; string name;
cout << "Please enter your name and an integer: ";
cin >> name >> n;
if(n == 0) cout << name;
if(n >= 100) exit(1);
if(n % 5 == 1) cout << name << name;
else while (n > 7) {
cout << n;
n = n - 2;
}
cout << endl;
return 0;
}
(i) The user enters: Freddy 0
Answer:
Freddy
(ii) The user enters: 007 6
Answer:
007007
(iii) The user enters: Fred 10
Answer:
108
(iv) The user enters: 9 11
Answer:
99
(v) The user enters: Freddy 111
Answer:

Problem 164
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program terminates.
3. The program prints a table with n lines of output. On output line number x the program should list the numbers
from 1 to x together with their sum.
For example, if the user enters 7 for n the program should print the following table.

1
1
1
1
1
1
1

the sum is 1
2 the sum is 3
2 3 the sum is 6
2 3 4 the sum is 10
2 3 4 5 the sum is 15
2 3 4 5 6 the sum is 21
2 3 4 5 6 7 the sum is 28

Answer:
#include <iostream>
using namespace std;
int main() {
int r, c, n;
cout << "How many rows of output do you want: ";
cin >> n;
if (n <= 0) exit(1);
for (r = 1; r <= n; r++) {
int sum = 0;
for (c = 1; c <= r; c++) {
cout << c << " ";
sum = sum + c;
}
cout << "the sum is " << sum << endl;
}
return 0;
}
Problem 165
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer x.
2. The program reads the number entered by the user. If x is not a positive integer, the program should terminate.
3. The program prints a countdown from x to 1.
Here is an example of how the program should work:
Enter a positive integer:
5 4 3 2 1

Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) exit(1);
for (int n = x; n >= 1; n--) cout << n << " ";
cout << endl;
return 0;
}
Problem 166
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
int print1(int x){
cout << "Odd" << endl;
return 1;
}
int print2(int x){
cout << x*x << endl;
return x;
}
int main(){
int n;
cout << "Please enter a positive integer: ";
cin >> n;
if(n <= 0){
cout << "No good!" << endl; exit(1);
}
if (n < 10) {
cout << n % 2 << endl; exit(0);
}
if (n > 11) cout << print1(n) << endl;
if (n % 2 == 1) print2(n);
else print1(n);
return 0;
}
(i) The user enters: 0
No good!
(ii) The user enters: 9
1
(iii) The user enters: 10
Odd
(iv) The user enters: 11
121
(v) The user enters: 12
Odd
1
Odd

Problem 167
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print (to the output screen) the message:
Very Easy Question

cout << "Very Easy Question" << endl;


(b) Print (to the output screen) the square root of 11:
cout << sqrt(11.0) << endl;
(c) Make the user enter 6 decimal values and print their product.
double x, product = 1.0;
for (int c = 1; c <= 6; c++) {
cin >> x;
product *= x;
}
cout << product << endl;
(d) The header line for a function add3 that calculates the sum of three input integer values. (A header line is a title
line, or prototype.)
int add3(int x, int y, int z)
(e) Print the value of a randomly selected integer between 31 and 41. (The program should make a random selection
using the function rand. Output values of 31 and 41 are allowed.).
cout << rand() % 11 + 31 << endl;;

Problem 168
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an even positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints an n n pattern of symbols in the shape of a large letter U .
For example, if the user enters 6 for n the program should print the following picture.
*
*
*
*
*
*
*
*
*
*
******
Answer:
#include <iostream>
using namespace std;
int main() {
int c, r, n;
cout << "Enter a positive
cin >> n;
while ((n <= 0) || (n % 2
cout << "Illegal. Try
cin >> n;
}
for (r = 1; r < n; r++) {
cout << "*";
for (c = 2; c < n; c++)

even integer: ";


!= 0)) {
again: ";

cout << " ";

cout << "*" << endl;


}
for (c = 1; c <= n; c++) cout << "*";
cout << endl;
return 0;
}
Problem 169
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer x.
2. The program reads the number entered by the user. If x is not a positive integer, the program should terminate.
3. The program repeatedly prints the word Hello a total of x times.
Here is an example of how the program should work:
Enter a positive integer:
Hello Hello Hello

Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) exit(1);
for (int n = 1; n <= x; n++) cout << "Hello ";
cout << endl;
return 0;
}
Problem 170
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
void print1(int x){
cout << "Odd" << endl;
}
void print2(int x){
cout << "Even" << endl;
}
int main(){
int n;
cout << "Please enter an integer: ";
cin >> n;
if(n == 0) cout << "Hello" << endl;
if(n <= 10) cout << "Goodbye" << endl;
if(n > 10 && n%2 == 1) print1(n);
if(n > 10 && n%2 == 0) print2(n);
if (n < 0) print2(n);
return 0;
}

(i) The user enters: 12


Even
(ii) The user enters: 11
Odd
(iii) The user enters: 10
Goodbye
(iv) The user enters: 0
Hello
Goodbye
(v) The user enters: -1
Goodbye
Even

Problem 171
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print (to the output screen) the message:
Easy Question
cout << "Easy Question" << endl;
(b) Print (to the output screen) a message made from the first 20 integers:
1234567891011121314151617181920
for (int n = 1; n <= 20; n++) cout << n;
cout << endl;
(c) Make the user enter 6 decimal values and print their sum.
double x, sum = 0.0;
for (int c = 1; c <= 6; c++) {
cin >> x;
sum += x;
}
cout << sum << endl;
(d) The header line for a function max3 that calculates the maximum of three input decimal values. (A header line
is a title line, or prototype.)
double max3(double x, double y, double z)
(e) Print the value of a randomly selected teen age. (The program should make a random selection using the function
rand. A teen age is a number between 13 and 19. ).
cout << rand() % 7 + 13 << endl;;

Problem 172
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an odd positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints an n n pattern of symbols in the shape of a large letter T .
For example, if the user enters 7 for n the program should print the following picture.
*******
*
*
*
*
*
*
Answer:
#include <iostream>
using namespace std;
int main() {
int c, r, n;
cout << "Enter a positive odd integer: ";
cin >> n;
while ((n <= 0) || (n % 2 == 0)) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (c = 1; c <= n; c++) cout << "*";
cout << endl;
for (r = 2; r <= n; r++) {
for (c = 1; c <= n / 2; c++) cout << " ";
cout << "*" << endl;
}
return 0;
}

Problem 173
Write a complete C++ program that does the following.
1. It asks the user to enter a positive number x.
2. The program reads the number entered by the user. If x is not positive, the program should terminate.
3. The program prints the square root of x.
Here is an example of how the program should work:
Enter a positive number:
The square root is: 2.5

6.25

Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x;
cout << "Enter a positive number: ";

cin >> x;
if (x <= 0) exit(0);
cout << "The square root is:
return 0;

" << sqrt(x) << endl;

Problem 174
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
for (int i = 1; i <= x % 10; i++)
cout << x << i << ".";
cout << x/10 << endl;
}
(i) The user enters: 0
Answer:
Illegal
(ii) The user enters: 1
Answer:
11.0
(iii) The user enters: 11
Answer:
111.1
(iv) The user enters: 44
Answer:
441.442.443.444.4
(v) The user enters: 40
Answer:
4

Problem 175
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print (to the output screen) the message:
2+2=4
Answer:
cout << "2 + 2 = 4" << endl;
(b) Read and store a first name, a middle initial, and a last name as entered by the user.
Answer:

string first, middle, last;


cin >> first >> middle >> last;
(c) Make the user enter 6 integer values and print the product.
Answer:
int x, product = 1;
for (int i = 1; i <= 6; i++) {
cin >> x;
product *= x;
}
cout << product;
(d) Print the message odd if the integer variable x stores an odd value, otherwise print the message even.
Answer:
if ((x % 2) == 1) cout << "odd" << endl;
else cout << "even" << endl;
(e) Print the value of a randomly selected two digit integer. (The program should make a random selection using
the function rand).
Answer:
cout << rand() % 90 + 10 << endl;

Problem 176
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter an odd positive integer.
2. The program reads a value n entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of n has been entered.
3. The program prints an n n pattern in the shape of a star. The pattern should appear as a large X printed from
copies of the letter X that lies over a large + printed from copies of the character +.
For example, if the user enters 7 for n the program should print the following picture.
X + X
X + X
X+X
+++X+++
X+X
X + X
X + X
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an odd positive integer: ";
cin >> n;
while ((n <= 0) || (n % 2 == 0)) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (int r = 1; r <= n; r++) {

for (int c = 1; c <= n; c++)


if (c == r || ((c + r) == (n + 1))) cout << "X";
else if ((c == (n + 1) / 2) || (r == (n + 1) / 2)) cout << "+";
else cout << " ";
cout << endl;
}
return 0;
}

Problem 177
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(a) Print (to the output screen) the message:
Problem 1(a).
Answer:
cout << "Problem 1(a)." << endl;
(b) Read and store a name as entered by the user.
Answer:
string name;
cout << "Enter your name: ";
cin >> name;
(c) Print the value of the larger of two variables x and y each of which has type int. (For example, if x is 0 and y is
3, the larger value 3 is printed.)
Answer:
if (x > y) cout << x;
else cout << y;
(d) Make the user enter 10 integer values and print the sum.
Answer:
cout << "Enter 10 integers: ";
int x, sum = 0;
for (int c = 1; c <= 10; c++) {
cin >> x;
sum = sum + x;
}
cout << sum << endl;

Problem 178
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer x.
2. The program reads the number entered by the user. If x is not positive, the program should terminate.
3. The program prints x randomly generated dice rolls.
Here is an example of how the program should work:
Enter a positive number:
The dice rolled: 4 1 6
Answer:
#include <iostream>
#include <stdlib.h>

#include <time.h>
using namespace std;
int main() {
srand(time(0));
int x;
cout << "Enter a positive number:";
cin >> x;
if (x <= 0) exit(0);
cout << "The dice rolled: ";
for (int c = 1; c <= x; c++)
cout << rand()%6 + 1 << " ";
cout << endl;
return 0;
}

Problem 179
The following C++ program applies 5 different functions. Supply title lines (prototypes) for the
5 functions. Do not supply any blocks of code for the functions.
int main() {
int x, c, r;
x = readData();
for (c = 0; c < 5; c++) printValues(x, c);
x = adjust(x + 2);
r = max3(x, c, 10);
return fun(x + c, r - c);
}
(a)
Answer:
int readData()
(b)
Answer:
void printValues(int a, int b)
(c)
Answer:
int adjust(int a)
(d)
Answer:
int max3(int a, int b, int c)
(e)
Answer:
int fun(int a, int b)

Problem 180
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer.

2. The program reads a value x entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of x has been entered.
3. The program prints a triangular display which has the number x on its top row. Each later row is obtained by
omitting the last digit from the number on the previous row.
For example, if the user enters 19683 for x the program should print the following picture.
19683
1968
196
19
1
Answer:
#include <iostream>
using namespace std;
int main() {
int x = 0;
while (x <= 0) {
cout << "Enter a positive integer: ";
cin >> x;
}
while (x > 0) {
cout << x << endl;
x = x / 10;
}
return 0;
}

Problem 181
Write a complete C++ program that does the following.
1. It asks the user to enter a positive even integer.
2. The program reads the number entered by the user. If the value is illegal, the program should terminate.
3. The program calculates and prints the square of the number.
Here is an example of how the program should work:
Enter a positive even number:
The square is 36.

Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive even integer: ";
cin >> n;
if ((n <= 0) || (n % 2 == 1)) exit(1);
int ans = n * n;
cout << "The square is " << ans << "." << endl;
return 0;
}

Problem 182
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.

(i) Print (to the output screen) the message:


Today is March 19, 2008.
Answer:
cout << "Today is March 19, 2008." << endl;
(ii) Read and store an age entered by the user.
Answer:
int age;
cout << "How old are you? ";
cin >> age;
(iii) Print the average 2 variables x and y each of which has type int. (For example, if x is 0 and y is 3, the average
is 1.5 and a decimal number must be printed.)
Answer:
cout << (x + y) / 2.0 << endl;
(iv) Make the user repeatedly enter a value for an integer variable x until the value entered is larger than 10.
Answer:
int x = 0;
while (x <= 10) {
cout << "Enter a value of x that is larger than 10: ";
cin >> x;
}

Problem 183
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
int fun1(int x) {
int ans = x / 10;
return ans;
}
void fun2(int x) {
cout << x << "* ";
}
void fun3(int x) {
cout << "fun3 ";
}
int main() {
int x;
cout << "Enter an integer: ";
cin >> x;
if (x < 10) {
cout << "Too small!" << endl; exit(1);
}
if (x == 10) fun3(x);
if (x >= 20) fun2(x);
if (x <= 20) cout << fun1(x);
cout << endl;
return 0;
}

(i) The user enters: 5


Answer:
Too small!
(ii) The user enters: 15
Answer:
1
(iii) The user enters: 25
Answer:
25*
(iv) The user enters: 10
Answer:
fun3 1
(v) The user enters: 20
Answer:
20* 2

Problem 184
Write a complete C++ program that does the following. (Programs that correctly carry out some of the tasks will
receive partial credit.)
1. It asks the user to enter an integer that is at least 2.
2. The program reads a value x entered by the user. If the value is not legal, the program repeatedly makes the user
type in another value until a legal value of x has been entered. (Note legal means greater than 1.)
3. The program prints a picture with x rows. The first row should show the first x positive integers, the next row
the first x 1 positive integers, until eventually the last row shows only the number 1.
For example, if the user enters 5 for x the program should print the following picture.
12345
1234
123
12
1
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a value greater than 1 for x: ";
cin >> x;
while (x <= 1) {
cout << "Try again: ";
cin >> x;
}

for (int r = x; r >= 1; r--) {


for (int c = 1; c <= r; c++) cout << c;
cout << endl;
}
return 0;
}

Problem 185
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
if (x <= 100) {
cout << x;
}
else {
cout << x/100 << x%10 << endl;
}
return 0;
}
(i) The user enters: -50
Answer: Illegal
(ii) The user enters: 0
Answer: Illegal
(iii) The user enters: 99
Answer: 99
(iv) The user enters: 456
Answer: 46
(v) The user enters: 4560
Answer: 450

Problem 186
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print (to the output screen) the message:
Easy!
Answer:
cout << "Easy!" << endl;
(ii) Read and store a name entered by the user.
Answer:
string name;
cout << "Who are you:";
cin >> name;

(iii) Print the value of the larger of 2 variables x and y each of which has type double.
Answer:
if (x > y) cout << x;
else cout << y;
(iv) Print the difference between 2 variables a and b each of which has type int. (The printed difference should not
be negative. For example the difference between 4 and 7 is 3, so too is the difference between 7 and 4.)
Answer:
if (a > b) cout << a - b;
else cout << b - a;

Problem 187
Write a complete C++ program that does the following.
1. It asks the user to enter a positive number.
2. The program reads the number entered by the user. If the value is not positive, the program should terminate.
3. The program calculates and prints the last digit of the entered number.
Here is an example of how the program should work:
Enter a positive number:
last digit is 6.

56

Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive number: ";
cin >> x;
if (x <= 0) exit(1);
cout << "last digit is " << x % 10 << endl;
return 0;
}

Problem 188
Write a complete C++ program that does the following. (Programs that correctly carry out some of the tasks will
receive partial credit.)
1. It asks the user to enter a positive integer value, x.
2. The program reads a value entered by the user. If the value is not positive, the program repeatedly makes the
user type in another value until a positive value of x has been entered. (Note positive means greater than 0.)
3. The program prints an x x rectangle outlined with symbols.
For example, if the user enters 5 for x the program should print the following pattern.
*****
*
*
*
*
*
*
*****
Answer:

#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive number: ";
cin >> x;
while (x <= 0) {
cout << "Enter a positive number: ";
cin >> x;
}
for (int row = 1; row <= x; row ++) {
for (int col = 1; col <= x; col++)
if (row == 1 || row == x || col == 1 || col == x) cout << "*";
else cout << " ";
cout << endl;
}
}

Problem 189
The following C++ program is supposed to ask a user to enter their name and date of birth. It
then greets the user and wishes a happy birthday if it is the users birthday. The program has a number of errors.
Rewrite the program to fix the errors.
#Include <iostream>;
#Include <string>;
Using namespace std;
main() {
cout "Enter your name and the month, day, and year of your birth: "
int name, month, day, year; cin >> name >> day >> month >> year;
cout "Hello name" endl; if (month = 3 || day = 14) {
cout "Happy birthday" endl;
}
Answer:
#include <iostream>
using namespace std;
int main() {
cout << "Enter your name and the month, day, and year of your birth: ";
string name;
int month, day, year;
cin >> name >> month >> day >> year;
cout << "Hello " << name << endl;
if (month == 3 && day == 14)
cout << "Happy birthday" << endl;
return 0;
}
Problem 190
Write a complete C++ program that does the following. (Programs that correctly carry out some of the tasks will
receive partial credit.)
1. It asks the user to enter a positive integer value, x.
2. The program reads a value entered by the user. If the value is not positive, the program repeatedly makes the
user type in another value until a positive value of x has been entered. (Note positive means greater than 0.)

3. The program calculates and prints out xx . (The value of xx is x x x . . . x, a product of x copies of the
number x.)
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
while (x <= 0) {
cout << "Please enter a positive value: ";
cin >> x;
}
int power = 1;
for (int i = 1; i <= x; i++)
power = power * x;
cout << power << endl;
return 0;
}

Problem 191
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
while (x > 0) {
cout << x % 10;
x = x / 10;
}
cout << x << endl;
return 0;
}
(i) The user enters: -50 Answer: Illegal
(ii) The user enters: 7 Answer: 70
(iii) The user enters: 467 Answer: 7640
(iv) The user enters a positive integer. (Explain how the output is related to the integer that the user enters.)
Answer: The digits of the input number are printed in reverse order, followed by a 0.

Problem 192
The following C++ program is supposed to ask a user to enter three different integers. It then
prints the middle value of the three input numbers. The program has several errors. Rewrite the program to fix the
errors and arrange the program so that it is easier for a human to read.

# <iostream>;
using namespace std;
int main
{
int x, y, z;
cout << "Enter three different integers: " endl;
cin >> "x" >> "y" >> "z" endl;
if ((x > y > z) && (z > y > x)); cout << y;
if ((y > x > z) && (z > x > y)); cout << x;
if ((z > y > x) && (x > y > z)); cout << y; return; };

Answer:
#include <iostream>
using namespace std;
int main() {
int x, y, z;
cout << "Enter three different integers: "
cin >> x >> y >> z;
if ( (x > y && y > z) || (z > y && y > x))
if ( (y > z && z > x) || (x > z && z > y))
if ( (z > x && x > y) || (y > x && x > z))
cout << endl;
return 0;
}

<< endl;
cout << y;
cout << z;
cout << x;

Problem 193
Write a complete C++ program that does the following.
1. It asks the user to enter a positive integer value, x.
2. The program reads a value entered by the user. If the value is not positive, the program repeatedly makes the
user type in another value until a positive value of x has been entered. (Note positive means greater than 0.)
3. The program prints out a triangle with x rows that points downwards. For example, if the user enters 3 for x the
program should print:
***
**
*
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
while (x <= 0) {
cout << "Enter a positive integer:";
cin >> x;
}
for (int row = x; row >= 1; row--) {
// print
for (int star = 1; star <= row; star++)
cout << "*";

row stars

cout << endl;


}
return 0;
}

Problem 194
Consider the following C++ program. Write the exact output that is produced in response to the
given user inputs.
#include <iostream>
using namespace std;
int fun(int a) {
int b; b = a / 10; return b;
}
int main() {
int x, y;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl; exit(1);
}
y = fun(x);
cout << x << y << endl;
return 0;
}
(i) The user enters: -50 Answer: Illegal
(ii) The user enters: 7 Answer: 70
(iii) The user enters: 467 Answer: 46746

Problem 195
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print (to the output screen) the greeting:
Hello. This is an easy question.
Answer:
cout << " Hello.

This is an easy question." << endl;

(ii) Get the user to enter their first name, which is to be stored as the variable name.
Answer:
string name;
cout << " Enter your first name:";
cin >> name;
(iii) Print the sum of the numbers from 1 to 1000 onto the screen. (The output should be the value of 1 + 2 + . . . +
999 + 1000).
Answer:
int sum = 0;
for (int c = 1; c <= 1000; c++)
sum = sum + c;
cout << sum << endl;

(iv) Get the user to enter an integer value. Print the message POSITIVE if it is greater than zero, or NEGATIVE
if it is less than zero. Do not take any action if the user enters zero.
Answer:
int x;
cout << "Enter an integer: ";
cin >> x;
if (x > 0) cout << "POSITIVE" << endl;
if (x < 0) cout << "NEGATIVE" << endl;

Problem 196
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

#include <iostream>
using namespace std;
void multiPrint(int y) {
for (int i = 1; i <= y; i++)
cout << y << "!";
return;
}
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
if (x > 2) multiPrint(x);
cout << x << endl;
return 0;
}
(i) The user enters: -50
Answer:
Illegal
(ii) The user enters: 1
Answer:
1
(iii) The user enters: 4
Answer:
4!4!4!4!4

Problem 197
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter their 4 digit id number.
2. The program reads the number entered by the user and stores it as the variable x. If the value of x is not in the
range from 1000 to 9999, the program repeatedly makes the user type in another number until a proper id number
has been entered.
3. The program calculates and prints out the last digit of the id number.

For example: A typical interaction with a user might be as follows. (The user responses are shown as bold.)
Enter your 4 digit id: 56789
Illegal, try again: 25
Illegal, try again: 9995
The last digit of your id is 5.
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter your 4 digit id: ";
cin >> x;
while ( x < 1000 || x > 9999) {
cout << "Illegal, try again: ";
cin >> x;
}
cout << "The last digit of your id is " << x % 10 << endl;
return 0;
}
Problem 198
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print (to the output screen) the greeting:
Hello. Today is 10/25/2006.
Answer:
cout << "Hello.

Today is 10/25/2006." << endl;

(ii) Get the user to enter their age, which is to be stored as the variable age.
Answer:
int age;
cout << "How old are you: ";
cin >> age;
(iii) Get the user to enter a positive integer value. Exit if the user enters a non-positive value, otherwise:
Print the message EVEN if the value is even, or ODD if it is odd.
Answer:
int x;
cout << "Enter a positive value: ";
cin >> x;
if (x <= 0) exit(0);
if ((x % 2) == 0) cout << "EVEN" << endl;
else cout << "ODD" << endl;
(iv) Get the user to enter a name. If the user enters the name Freddy, tell the user to enter a different name and
force the user to re-enter a name until it is different from Freddy.
Answer:
string name;
cout << "What is your name? ";
cin >> name;
while (name == "Freddy") {
cout << "That name is illegal.
cin >> name;
}

Give another: ";

Problem 199
user inputs.

Consider the following C++ program. Explain what output is produced in response to the given

int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
if (x < 10) cout << x--;
else {
if ((x % 10) == 0) cout << x / 10;
cout << x * 10;
}
cout << endl;
}
(i) The user enters: -50
Answer: Illegal
(ii) The user enters: 0
Answer: Illegal
(iii) The user enters: 9
Answer: 9
(iv) The user enters: 456
Answer: 4560
(v) The user enters: 4560
Answer: 45645600

Problem 200
Write a complete C++ program that does the following. (Programs that correctly carry out some
of the tasks will receive partial credit.)
1. It asks the user to enter a positive integer value, x.
2. The program reads a value entered by the user. If the value is not positive, the program repeatedly makes the
user type in another value until a positive value of x has been entered. (Note positive means greater than 0.)
3. The program prints a triangular pattern that is x rows high. The characters 0 and 1 are used to print the pattern.
Odd numbered rows are printed using a 1 and even numbered rows are printed using a 0.
For example, if the user enters 4 for x the program should print the following pattern with 4 rows.
1
00
111
0000
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Give a positive integer value of x: ";
cin >> x;
while (x <= 0) {

cout << "Give a POSITIVE value: ";


cin >> x;
}
for (int r = 1; r <= x; r++) {
for (int c = 1; c <= r; c++)
cout << r % 2;
cout << endl;
}
}

Problem 201
Write C++ statements to carry out the following tasks. Do not write complete programs, just
give a single line, or a few lines of C++ instructions.
(i) Print (to the output screen) the greeting:
Hello.
Answer:
cout << "Hello." << endl;
(ii) Get the user to enter their age. Then print out whichever the following messages applies:
You are over 25
You are not over 25
Answer:
int age;
cout << "Enter your age: ";
cin >> age;
if (age > 25) cout << "You are over 25";
else cout "You are not over 25";
cout << endl;
(iii) Ask the user to enter an even number.
Make the user re-eneter the number as often as is needed until the number is even.
Answer:
int number;
cout << "Enter an even number: ";
cin >> number;
while (number % 2 != 0) {
cout << "Try again: ";
cin >> number;
}
(iv) Print the average value of 3 variables x, y, and z each of which has type double.
Answer:
cout << (x + y + z) / 3;
(v) Calculate and print the decimal that represents the fraction 17 .
Answer:
cout << 1.0 / 7;

Problem 202
Write a complete C++ program that does the following.
1. It asks the user to enter a number of cents that is betwee 0 and 99.

2. The program reads the number entered by the user. If the value is not in the right range, the program should
terminate.
3. The program calculates and prints out the most efficient combination of quarters, nickels, dimes, and pennies that
provide the sum entered by the user.
Here is an example of how the program should work:
How many cents? 57
quarters: 2
dimes: 0
nickels: 1
pennies: 2
Answer:
#include <iostream>
using namespace std;
int main() {
int cents, q, d, n;
cout << " How many cents? ";
cin >> cents;
if (cents < 0 || cents > 99) exit(1);
q = cents / 25;
cents = cents % 25;
d = cents / 10;
cents = cents % 10;
n = cents / 5;
cents = cents % 5;
cout << "quarters: " << q << endl
<< "dimes: " << d << endl
<< "nickels: " << n << endl
<< "pennies: " << cents << endl;
return 0;
}

Problem 203
Write a complete C++ program that does the following. (Programs that correctly carry out some of the tasks will
receive partial credit.)
1. It asks the user to enter a positive integer value, x.
2. The program reads a value entered by the user. If the value is not positive, the program repeatedly makes the
user type in another value until a positive value of x has been entered. (Note positive means greater than 0.)
3. The program prints an x x square pattern of symbols in such a way that rows and columns are separated by
rows and columns of symbols.
For example, if the user enters 3 for x the program should print the following pattern (there are 3 rows and 3 columns
that contain s, but there are other rows and columns with only s).
*-*-*
----*-*-*
----*-*-*
Answer:

#include <iostream>
using namespace std;
int main() {
int x, r, c;
cout << "Enter a positive integer: ";
cin >> x;
while (x <= 0) {
cout << "Try again: ";
cin >> x;
}
for (r = 1; r <= x; r++) {
for (c = 1; c <= x; c++) {
cout << "*";
if (c < x) cout << "-";
else cout << endl;
}
if (r < x) {
for (c = 1; c <= 2 * x - 1; c++) cout << "-";
cout << endl;
}
}
}

You might also like