Prac 1 Sol
Prac 1 Sol
Prac 1 Sol
of either 3 or 4 when it is divided by 7. If the user gives incorrect input, the program should ask the user to try
again as often as necessary. When the user succeeds, the program should report how many attempts were needed.
Sample output might be:
Answer:
#include <iostream>
using namespace std;
int main() {
int x, attempts = 1;
cout << "Enter a positive number that is 3 or 4 modulo 7: ";
cin >> x;
while(x <= 0 || x % 7 < 3 || x % 7 > 4) {
cout << "Wrong. Try again: ";
cin >> x;
attempts++;
}
cout << "Good you passed after " << attempts << " attempts.\n";
return 0;
}
Problem 2
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume that int variables x, y, z have been declared and have
legal values
(b) Print the (integer) quotient that is found when y is divided by the product of x and z.
Answer:
(e) Print all the numbers from 1 to z. (Put all numbers on the same line. Put spaces between numbers).
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x = 4, y = 4, z = 11, w = 7;
string a = "x", b = "y", c = "a";
cout << a << "a" << "x" << a << endl; // line (b)
if (x == y) cout << "x" << "==" << y << endl; // line (c)
if ((x == y) || (a == b)) cout << x << "==" << "y" << endl; // line (d)
while (x <= w) {x++; cout << x;} cout << endl; // line (e)
xa411
xaxx
x==4
4==y
Problem 4 Write a complete C++ program that asks the user to enter an odd positive integer n. If n is even or
negative the program should exit. Otherwise the program should print a triangle with n rows and (n + 1)/2 columns
that points to the right.
For example, if the user specified 9 for n, the program would print as follows:
*
**
***
****
*****
****
***
**
*
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Type an odd positive integer: ";
cin >> n;
if (n <= 0 || n % 2 == 0) return 0;
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= (n + 1)/2; c++) {
if (c <= r && c + r <= (n + 1)) cout << "*";
else cout << " ";
}
cout << endl;
}
return 0;
}
Problem 5 Write a complete C++ program that asks the user to enter a positive integer x. If the user enters a
non-positive number the program should ask the user to try again as often as necessary. After the user has entered
a positive value, the program should find (but not print) the remainders when x is divided by 7 and 8. It should
then print the larger of these two remainders. (In case the two remainders are equal either can be printed.)
Sample output might be:
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive number: ";
cin >> x;
while(x <= 0) {
cout << "Not positive. Try again: ";
cin >> x;
}
cout << "Bigger remainder is: ";
if (x % 7 > x % 8) cout << x % 7;
else cout << x % 8;
cout << endl;
return 0;
}
Problem 6
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume that int variables x, y, z have been declared and have
legal values
(a) Print z copies of the word Hello. Make each copy on its own line of output.
Answer:
z = y / x;
int main() {
int x = 7, y = 7, z = 11, w = 1;
string a = "b", b = "a", c = "y";
cout << a << "a" << "x" << a << endl; // line (b)
if (x == y) cout << "x" << "==" << y << endl; // line (c)
if ((x == y) && (a == b)) cout << x << "==" << "y" << endl; // line (d)
while (x >= w) {x -= 2; cout << x;} cout << endl; // line (e)
by711
baxb
x==7
531-1
Problem 8 Write a complete C++ program that asks the user to enter a positive integer n. If n is not positive
the program should exit. Otherwise the program should print n triangles in a vertical stack. Each triangle should
have 2n − 1 columns and n rows and should points upwards.
For example, if the user specified 3 for n, the program should print as follows:
*
***
*****
*
***
*****
*
***
*****
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Type a positive integer: ";
cin >> n;
if (n <= 0) return 0;
for (int triangle = 1; triangle <= n; triangle ++) {
for (int r = n; r >= 1; r--) {
for (int c = 1; c <= 2*n -1; c++) {
if (c < r || c + r > 2*n) cout << " ";
else cout << "*";
}
cout << endl;
}
}
return 0;
}
Problem 9 Write a complete C++ program that asks the user to enter an odd positive integer n. If n is even
or negative the program should exit. Otherwise the program should print a triangle with n columns and (n + 1)/2
rows that points upwards.
For example, if the user specified 9 for n, the program would print as follows:
*
***
*****
*******
*********
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Type an odd positive integer: ";
cin >> n;
if (n <= 0 || n % 2 == 0) return 0;
for (int r = (n + 1) / 2; r >= 1; r--) {
for (int c = 1; c <= n; c++) {
if (c < r || c + r > (n + 1)) cout << " ";
else cout << "*";
}
cout << endl;
}
return 0;
}
Problem 10 Write a complete C++ program that prints the numbers from 28 to 387 with 10 numbers (separated
by spaces) on each line.
The output from your program should begin
28 29 30 31 32 33 34 35 36 37
38 39 40 41 42 43 44 45 46 47
Answer:
#include <iostream>
using namespace std;
int main() {
for (int n = 28; n < 388; n++) {
cout << n << " ";
if (n % 10 == 7) cout << endl;
}
return 0;
}
Problem 11
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int x = 18;
(a) Print to the user’s screen the sentence:In C++ an endl makes a new line.
Answer:
cout << "In C++ an endl makes a new line." << endl;
(d) Print all numbers less that 1000 that are either divisible 7 or are even and greater than 400.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 9.0, y = 16.0, z = 25.0;
string a = "b", b = "a";
cout << sqrt(z) << endl; // line (a)
cout << sqrt(sqrt(y)) << endl; // line (b)
if ((x + y) != z) cout << b << endl; // line (c)
cout << a << "a" << "b" << b << endl; // line (d)
if (a == "b") cout << z; else cout << x; // line (e)
cout << endl;
}
baba
25
Problem 13 Write a complete C++ program that asks the user for a number n and prints 2 large copies of an
X pattern (each with height n) in a horizontal sequence.
For example, if the user specified 5 for n, the program would print as follows:
* ** *
* * * *
* *
* * * *
* ** *
(Each X pattern should begin in the column after the previous one ends. Do not try to check whether the user input
is legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
Problem 14 Write a complete C++ program that prints the numbers from 980 down to 666 with 6 numbers
(separated by spaces) on each line.
The output from your program should begin
Answer:
#include <iostream>
using namespace std;
int main() {
for (int n = 980; n >= 666; n--) {
cout << n << " ";
if (n % 6 == 3) cout << endl;
}
cout << endl;
return 0;
}
Problem 15
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int y = 12;
(a) Print to the user’s screen the sentence:C++ output uses cout.
Answer:
(d) Print all numbers less that 1000 that end in a 7 and are divisible by 3.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 9.0, y = 16.0, z = 25.0;
string a = "a", b = "a";
cout << sqrt(y) << endl; // line (a)
cout << sqrt(y) + sqrt(x) << endl; // line (b)
if ((x + y) == z) cout << b << endl; // line (c)
cout << a << "a" << "b" << b << endl; // line (d)
if (a == "b") cout << z; else cout << x; // line (e)
cout << endl;
}
aaba
Problem 17 Write a complete C++ program that asks the user for a number n and prints 3 large copies of an
L pattern (each with height n) in a horizontal sequence.
For example, if the user specified 4 for n, the program would print as follows:
* * *
* * *
* * *
**** **** ****
(Each L pattern should begin after a gap of one column after the previous one ends. Do not try to check whether
the user input is legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
Problem 18 Write a complete C++ program that prints the numbers from 28 to 387 with 10 numbers (separated
by commas) on each line.
The output from your program should begin
28,29,30,31,32,33,34,35,36,37
38,39,40,41,42,43,44,45,46,47
Answer:
#include <iostream>
using namespace std;
int main() {
for (int n = 28; n < 388; n++) {
cout << n;
if (n % 10 == 7) cout << endl;
else cout << ",";
}
return 0;
}
Problem 19
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int z = 5;
(a) Print to the user’s screen the words:endl makes a line and for makes a loop
Answer:
cout << "endl makes a line and for makes a loop" << endl;
(d) Print all three digit numbers that either end in a 7 or are even and divisible by 7.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 49.0, y = 81.0, z = 25.0;
string a = "ab", b = "ba";
cout << sqrt(x) << endl; // line (a)
cout << sqrt(sqrt(y)) << endl; // line (b)
if ((x + y) != z) cout << a << endl; // line (c)
cout << a << "a" << "b" << b << endl; // line (d)
if (a == "b") cout << x; else cout << y; // line (e)
cout << endl;
}
ab
ababba
81
Problem 21 Write a complete C++ program that asks the user for a number n and prints 2 large copies of an
E pattern (each with height n that is odd) in a horizontal sequence.
For example, if the user specified 5 for n, the program would print as follows:
***** *****
* *
***** *****
* *
***** *****
(Each E pattern should begin after a gap of one column after the previous one ends. Do not try to check whether
the user input is legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an odd number: ";
cin >> n;
Problem 22 Write a complete C++ program that prints the numbers from 980 down to 669 with 6 numbers
(separated by periods) on each line.
The output from your program should begin
980.979.978.977.976.975
974.973.972.971.970.969
Answer:
#include <iostream>
using namespace std;
int main() {
for (int n = 980; n >= 669; n--) {
cout << n;
if (n % 6 == 3) cout << endl;
else cout << ".";
}
cout << endl;
return 0;
}
Problem 23
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int y = 12;
(a) Print to the user’s screen the sentence:Quote Hello but do not quote cout.
Answer:
cout << "Quote Hello but do not quote cout." << endl;
(b) Print the square root of the square root of y.
Answer:
(d) Print all numbers less that 1000 that end in a 7 and are divisible by 7.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 81.0, y = 49.0, z = 36.0;
string a = "az", b = "za";
cout << sqrt(y) << endl; // line (a)
cout << sqrt(y) + sqrt(x) << endl; // line (b)
if ((x + y) == z) cout << b << endl; // line (c)
cout << a << "a" << "b" << b << endl; // line (d)
if (a == "b") cout << z; else cout << x; // line (e)
cout << endl;
}
16
azabza
81
Problem 25 Write a complete C++ program that asks the user for a number n and prints 3 large copies of a T
pattern (each with height n that is odd) in a horizontal sequence.
For example, if the user specified 5 for n, the program would print as follows:
(Each T pattern should begin after a gap of one column after the previous one ends. Do not try to check whether
the user input is legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an odd number: ";
cin >> n;
9
F ahrenheit = Celsius + 32
5
Here is an example of how the program should work:
Enter the temperature in degrees celsius: 60
Enter a different value: 60
Fahrenheit: 140.0
Answer:
#include <iostream>
using namespace std;
int main() {
double celsius, fahrenheit;
Problem 27
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int a, b;
string word, second;
(a) Ask for, and read the value for second from the user.
Answer:
(d) If word and second are equal print the value of a, otherwise print the value of b.
Answer:
if (word == second) cout << a << endl;
else cout << b << endl;
(e) Print a copies of the value of word on one line separated by single spaces.
Answer:
for (int x = 1; x <= a; x++) cout << word << " ";
cout << endl;
#include <iostream>
using namespace std;
int main() {
int p = 36, q = 49, r = 25;
string a = "yes";
string x = "no";
if (a == x) cout << p; else cout << q; // line (a)
cout << endl;
if ((p <= q) && (r <= q)) cout << "ok" << endl; // line (b)
if ((a == x) || ("x" == "x")) r+=5; cout << r << endl; // line (c)
if (!((p % 2) < (q % 2))) cout << "gg"; else cout << "ll"; // line (d)
cout << endl;
while (p > r) p--; cout << p << "\n"; // line (e)
}
49
ok
30
ll
30
Problem 29 Write a complete C++ program that does the following.
1. It asks the user to enter a temperature in degrees fahrenheit.
2. If the temperature f does not satisfy 0 ≤ f ≤ 100 the program should print the message Out of range and
terminate.
3. The program calculates and prints the celsius value of temperature using the formula:
5
Celsius = (F ahrenheit − 32)
9
Answer:
#include <iostream>
using namespace std;
int main() {
double celsius, fahrenheit;
Problem 30
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int x, y;
string name, message;
(a) Ask for, and read the value for name from the user.
Answer:
(d) If x is not greater than y print the value of name, otherwise print the value of message.
Answer:
(e) Print x copies of the value of x followed by y copies of the value of message.
Answer:
#include <iostream>
using namespace std;
int main() {
int p = 31, q = 23, r = 15;
string a = "abc";
string x = "abc";
if (a == x) cout << p; else cout << q; // line (a)
cout << endl;
if ((p <= q) && (p <= r)) cout << "a" << endl; // line (b)
if ((a == x) || ("a" == "x")) r++; cout << r << endl; // line (c)
if (!((p % 2) < (q % 2))) cout << "no"; else cout << "yes"; // line (d)
cout << endl;
while (p < r) p++; cout << p << "\n"; // line (e)
}
31
16
31
Problem 32 Write a complete C++ program that asks the user for a number n and prints n diagonal stripes
(each with height n and width n) in a horizontal sequence.
For example, if the user specified 4 for n, the program would print as follows:
* * * *
* * * *
* * * *
* * * *
(Each stripe should begin in the column after the previous one ends. Do not try to check whether the user input is
legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number n: ";
cin >> n;
1
Area = Base × Height
2
Answer:
#include <iostream>
using namespace std;
int main() {
double base, height;
double area;
Problem 34
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. No answer can use more than two lines. Assume the following variables have been declared and have
legal values
int x, y;
string name, message;
(a) Ask for, and read the value for name from the user.
Answer:
(d) If x is greater than y print the value of x, otherwise print the value of message.
Answer:
(e) Print x copies of the value of name followed by y copies of the value of message.
Answer:
for (int a = 1; a <= x; a++) cout << name;
for (int a = 1; a <= y; a++) cout << message;
#include <iostream>
using namespace std;
int main() {
int p = 11, q = 13, r = 15;
string a = "x";
string x = "xx";
if (a == x) cout << p; else cout << q; // line (a)
cout << endl;
if ((p <= q) && (p <= r)) cout << "a" << endl; // line (b)
if ((a == x) || ("a" == "x")) r++; cout << r << endl; // line (c)
if (!((p % 2) < (q % 2))) cout << "no"; else cout << "yes"; // line (d)
cout << endl;
while (p < r) p++; cout << p << "\n"; // line (e)
}
13
15
no
15
Problem 36 Write a complete C++ program that asks the user for a number n and prints n triangles (each
with height n) in a horizontal sequence.
For example, if the user specified 4 for n, the program would print as follows:
* * * *
** ** ** **
*** *** *** ***
****************
(Each triangle should begin in the column after the previous one ends. Do not try to check whether the user input
is legal or sensible.)
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number n: ";
cin >> n;
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer between 100 and 9999: ";
cin >> n;
while (n < 100 || n > 9999) {
cout << "Out of range. Try again: ";
cin >> n;
}
while (n > 0) {
cout << n % 10 << endl;
n = n / 10;
}
return 0;
}
Problem 38
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Your answers must be short and must fit in the spaces provided. Assume the following variables have
been declared
int x, y;
(a) Prompt for, and read values for x and y from the user.
Answer:
cout << "Enter 2 integers: ";
cin >> x >> y;
(b) Print x copies of the number y, without spaces on one line of output.
Answer:
for (int c = 1; c <= x; c++) cout << y; cout << endl;
(d) If x and y are both between 10 and 99 print 6 copies of the word "Hello" on a single line.
Answer:
if (x > 9 && x < 100 && y > 9 && y < 100)
for (int c = 1; c <= 6; c++) cout << "Hello ";
cout << endl;
#include <iostream>
using namespace std;
int main() {
int x = 7, y = 10, z = 65;
string freddy = "fred";
string fred = "freddy";
cout << "fred" << " " << fred << endl; // line (a)
cout << (z % y) / x << endl; // line (b)
if ((x > y) && (y > x)) cout << fred << endl; // line (c)
cout << fred << freddy << endl; // line (d)
cout << x << "*" << y << "=" << z << "\n"; // line (e)
}
(a) What is the output at line (a)?
Answer:
fred freddy
freddyfred
7*10=65
Problem 40 Write a complete C++ program that asks the user for a height h and prints a white X pattern
(made of spaces) against a dark background made of Xs.
For example, if the user specified 7 for h, the program would print as follows:
XXXXX
X XXX X
XX X XX
XXX XXX
XX X XX
X XXX X
XXXXX
Answer:
#include <iostream>
using namespace std;
int main() {
int h;
cout << "Enter a height h: ";
cin >> h;
for (int r = 1; r <= h; r++) {
for (int c = 1; c <= h; c++) {
if ((r == c) || ((r + c) == (h + 1))) cout << " ";
else cout << "X";
}
cout << endl;
}
return 0;
}
Problem 41 Write a complete C++ program that does the following.
1. It asks the user to enter a 2-digit integer.
2. If the entered number does not have 2 digits, the program forces the user to enter further integers until one with
2 digits is given.
3. The program then prints the bigger of the 2 digits in the user’s number (either digit in case they are the same).
Here is an example of how the program should work:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a 2-digit integer: ";
cin >> n;
while (n < 10 || n > 99) {
cout << "That does not have 2 digits. Try again: ";
cin >> n;
}
int a = n % 10, b = n / 10;
cout << "The bigger digit is ";
if (a > b) cout << a;
else cout << b;
cout << endl;
return 0;
}
Problem 42
Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of
C++ code. Your answers must be short and must fit in the spaces provided. Assume the following variables have
been declared
(a) Prompt for, and read values for x, y and name from the user.
Answer:
(b) If y is 0 print "Illegal Division" otherwise the exact value of x divided by y is printed
Answer:
if (x < 0) x = -x;
if (y < 0) y = -y;
(d) Say Hello to the user (eg "Hello Freddy") x times on x different lines.
Answer:
for (int c = 1; c <= x; c++) cout << "Hello " << name << endl;
#include <iostream>
using namespace std;
int main() {
int x = 7, y = 10, z = 65;
string freddy = "fred";
string fred = "freddy";
for (int c = x; c < y; c++) cout << freddy; cout << endl; // line (a)
cout << (z % y) / (y % x) << endl; // line (b)
if ((x > y) || (y > x)) cout << fred << endl; // line (c)
cout << fred << " * " << (y - x) << endl; // line (d)
cout << x << "%" << y << "=" << "x % y" << "\n"; // line (e)
}
fredfredfred
freddy
7%10=x % y
Problem 44 Write a complete C++ program that asks the user for a width w and prints a white arrow pattern
(made of spaces) against a dark background made of Xs.
For example, if the user specified 5 for w, the program would print as follows:
XXXX
X XXX
XX XX
XXX X
XXXX
XXX X
XX XX
X XXX
XXXX
Answer:
#include <iostream>
using namespace std;
int main() {
int w;
cout << "Enter a width w: ";
cin >> w;
for (int r = 1; r < 2*w; r++) {
for (int c = 1; c <= w; c++) {
if ((r == c) || ((r + c) == (2 * w))) cout << " ";
else cout << "X";
}
cout << endl;
}
return 0;
}
Problem 45 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 21:";
cin >> n;
Problem 46 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 15:";
cin >> n;
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 15:";
cin >> n;
Problem 48 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 21:";
cin >> n;
Problem 49 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 row’s number.
Here is an example of how the program should work:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
Problem 50 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 column’s
number.
Here is an example of how the program should work:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
Problem 51 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
row’s number.
Here is an example of how the program should work:
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;
}
Problem 52 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 column’s number.
Here is an example of how the program should work:
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer between 1 and 9:";
cin >> n;
Problem 53 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:
Upward
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 9:";
cin >> n;
Problem 54 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:
Evil
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 666:";
cin >> n;
Problem 55 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;
Problem 56 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:
Lucky
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 666:";
cin >> n;
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. 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:
95
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 9:";
cin >> n;
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. 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:
12
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 59 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:
954
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Give me an integer greater than 99:";
cin >> n;
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. 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:
12
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 61 Consider the following C++ program.
#include <iostream>
using namespace std;
int main() {
int x = 123;
cout << x % 10 << endl; // line (a)
cout << x / 10 << endl; // line (b)
if (x > 50) cout << "Big" << endl; // line (c)
cout << endl;
while (x > 0) { cout << "1"; x /= 10;} // line (d)
cout << endl;
cout << x << endl; // line (e)
}
12
Big
111
#include <iostream>
using namespace std;
int main() {
int x = 2345;
cout << x % 10 << endl; // line (a)
cout << x / 10 << endl; // line (b)
if (x > 5000) cout << "Big" << endl; // line (c)
cout << endl;
while (x > 0) { cout << "*"; x /= 10;} // line (d)
cout << endl;
cout << x + 5 << endl; // line (e)
}
(a) What is the output at line (a)?
Answer:
234
****
#include <iostream>
using namespace std;
int main() {
int x = 31;
cout << x % 10 << endl; // line (a)
cout << x / 10 << endl; // line (b)
if (x > 50) cout << "Big" << endl; // line (c)
cout << endl;
while (x > 0) { cout << "1"; x /= 10;} // line (d)
cout << endl;
cout << x * x << endl; // line (e)
}
11
#include <iostream>
using namespace std;
int main() {
int x = 5432;
cout << x % 10 << endl; // line (a)
cout << x / 10 << endl; // line (b)
if (x > 5000) cout << "Big" << endl; // line (c)
cout << endl;
while (x > 0) { cout << "A"; x /= 10;} // line (d)
cout << endl;
cout << x - 5 << endl; // line (e)
}
543
Big
AAAA
-5
int main() {
int y,x = 12;
cout << x + x * 10 << endl; // line (a)
cout << x / 100 << endl; // line (b)
for (y = 10; y < x; y++) cout << y; // line (c)
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; // line (d)
cout << endl;
cout << x << "*" << x << endl; // line (e)
}
132
1011
24
12*12
#include <iostream>
using namespace std;
int main() {
int y,x = 210;
cout << x + x * 10 << endl; // line (a)
cout << x / 100 << endl; // line (b)
for (y = 210; y < x; y++) cout << y; // line (c)
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; // line (d)
cout << endl;
cout << x << "*" << x << endl; // line (e)
}
210
210*210
#include <iostream>
using namespace std;
int main() {
int y,x = 13;
cout << x + x * 10 << endl; // line (a)
cout << x / 100 << endl; // line (b)
for (y = 10; y < x; y++) cout << y; // line (c)
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; // line (d)
cout << endl;
cout << x << "*" << x << endl; // line (e)
}
143
101112
13*13
#include <iostream>
using namespace std;
int main() {
int y,x = 211;
cout << x + x * 10 << endl; // line (a)
cout << x / 100 << endl; // line (b)
for (y = 210; y < x; y++) cout << y; // line (c)
cout << endl;
if (x > 50) cout << x; else cout << 2 * x; // line (d)
cout << endl;
cout << x << "*" << x << endl; // line (e)
}
2321
210
211
211*211
Problem 69
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:
if (y < 0) y = -y;
Problem 70
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;
(c) Read a new value for y and then for x from the user.
Answer:
y = x - y;
if (y < 0) y = -y;
Problem 71
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;
if (x < 0) y = -x;
else y = x;
Problem 72
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:
y = - x - y;
if (y < 0) y = -y;
Problem 73
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;
if (y < 0) return 0;
if (y < 0) y = -y;
Problem 74
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;
if (y > 0) return 0;
for (int c = 1; c <= -y; c++) cout << "y >= 0; " << endl;
y = x + y;
if (y < 0) y = -y;
Problem 75
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;
if (y < 0) return 0;
(b) Print x copies of the last digit of y on a single line of output.
Answer:
if (y < 0) y = -y;
Problem 76
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;
if (y > 0) return 0;
y = x - y;
if (y < 0) y = -y;
Problem 77 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 78 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 79 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 80 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 81 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.
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 82 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 83 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.
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 84 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.
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 85
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;
(c) Print the square root of 19. Use a C++ function for the calculation.
Answer:
(d) Print a random number in the range 23 to 34, inclusive. Use a C++ function.
Answer:
(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 86
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;
(c) Print a random number in the range 33 to 53, inclusive. Use a C++ function.
Answer:
a = a + b;
while (a > 0) {
cout << a % 10;
a = a / 10;
}
cout << endl;
Problem 87
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;
(c) Print the square root of the square root of 19. Use a C++ function for the calculation.
Answer:
(d) Print a random number in the range 123 to 126, inclusive. Use a C++ function.
Answer:
(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 88
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:
(c) Print a random number in the range 33 to 153, inclusive. Use a C++ function.
Answer:
(d) Print the square root of the cube of 91. Use a C++ function for the calculation.
Answer:
a = a * b;
while (a > 0) {
cout << a % 10;
a = a / 10;
}
cout << endl;
Problem 89
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;
(c) Print the square root of 5. Use a C++ function for the calculation.
Answer:
(d) Print a random number in the range 1 to 6, inclusive. Use a C++ function.
Answer:
Problem 90
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;
(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:
(d) Print the square root of 8. Use a C++ function for the calculation.
Answer:
x = x + y;
while (x >= 10) {
x = x / 10;
}
cout << x << endl;
Problem 91
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;
(c) Print the square root of 50. Use a C++ function for the calculation.
Answer:
(d) Print a random number in the range 3 to 8, inclusive. Use a C++ function.
Answer:
Problem 92
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;
(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:
x = y + z;
while (x >= 10) {
x = x / 10;
}
cout << x << endl;
#include <iostream>
using namespace std;
int main() {
int x = 4, y = 11, z = 91;
string freddy = "Fred";
string fred = "Freddy";
cout << "fred" << endl; // line (a)
cout << z / y << endl; // line (b)
if ((y > x) && (y > z)) cout << fred << endl; // line (c)
cout << fred << freddy << endl; // line (d)
cout << x << "x" << fred << "=" << z << "\n"; // line (e)
}
fred
FreddyFred
#include <iostream>
using namespace std;
int main() {
int x = 12, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Fred";
cout << z % y << endl; // line (a)
cout << fred << endl; // line (b)
cout << "fred" << freddy << endl; // line (c)
if ((x > y) && (y > z)) cout << freddy << endl; // line (d)
cout << x << y << "+" << z << "=" << x*y+z << "\n"; // line (e)
}
Fred
fredFreddy
Freddy
1211+9=141
#include <iostream>
using namespace std;
int main() {
int x = 4, y = 11, z = 9;
string freddy = "Fred";
string fred = "Freddy";
cout << "freddy" << endl; // line (a)
cout << (double) (z / x) << endl; // line (b)
if ((y > x) && (y > z)) cout << fred << endl; // line (c)
cout << fred << fred << endl; // line (d)
cout << x << "x" << fred << "=" << z << "\n"; // line (e)
}
(a) What is the output at line (a)?
Answer:
freddy
Freddy
FreddyFreddy
4xFreddy=9
#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; // line (a)
cout << fred << endl; // line (b)
cout << "freddy" << freddy << endl; // line (c)
if ((x > y) && (y > z)) cout << freddy << endl; // line (d)
cout << x << y << "+" << z << "=" << x*y+z << "\n"; // line (e)
}
Fred
freddyFreddy
(d) What is the output at line (d)?
Answer:
1211+19=151
#include <iostream>
using namespace std;
int main() {
int x = 14, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Fred";
cout << "fred" << endl; // line (a)
cout << z / y << endl; // line (b)
if ((y > x) && (y > z)) cout << fred << endl; // line (c)
cout << fred << freddy << endl; // line (d)
cout << x << "x" << fred << "=" << z << "\n"; // line (e)
}
fred
FredFreddy
14xFred=9
int main() {
int x = 12, y = 11, z = 19;
string freddy = "Fred";
string fred = "Freddy";
cout << z % y << endl; // line (a)
cout << fred << endl; // line (b)
cout << "fred" << freddy << endl; // line (c)
if ((x > y) && (y > z)) cout << freddy << endl; // line (d)
cout << x << y << "+" << z << "=" << x*y+z << "\n"; // line (e)
}
Freddy
fredFred
1211+19=151
#include <iostream>
using namespace std;
int main() {
int x = 14, y = 11, z = 9;
string freddy = "Freddy";
string fred = "Freddy";
cout << "freddy" << endl; // line (a)
cout << (double) (z / x) << endl; // line (b)
if ((y > x) && (y > z)) cout << fred << endl; // line (c)
cout << fred << fred << endl; // line (d)
cout << x << "x" << fred << "=" << z << "\n"; // line (e)
}
FreddyFreddy
14xFreddy=9
#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; // line (a)
cout << fred << endl; // line (b)
cout << "freddy" << freddy << endl; // line (c)
if ((x > y) && (y > z)) cout << freddy << endl; // line (d)
cout << x << y << "+" << z << "=" << x*y+z << "\n"; // line (e)
}
11
Fred
freddyFred
1912+11=239
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 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 user’s number.
(4) The program prints the cube of the user’s number.
An example run of the program follows.
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 102 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 user’s
number when it stops.
An example run of the program follows.
#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 103 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 user’s number.
(4) The program prints the square root of the user’s number.
An example run of the program follows.
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 104 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 user’s number when it stops.
An example run of the program follows.
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 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 two integers a and b with values between 1 and 20.
(2) If either of the user’s 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.
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 106 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 user’s 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.
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 107 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 user’s 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.
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 108 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 user’s 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.
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 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 two integers a and b with values between 1 and 20.
(2) If either of the user’s 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.
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 110 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 user’s 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.
#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 111 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 user’s 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.
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 112 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 user’s 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.
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 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) 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
pi is 22/7
Answer:
(iv) If the value of firstName is Freddy, print the message Goodbye. Otherwise print Hello.
Answer:
(v) Prompt the user to enter a first name and last name and read their response to appropriate variables.
Answer:
Problem 115 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;
}
Illegal -20
Goodbye!
10 1
10
Problem 116 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 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 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
x is greater than y
Answer:
(iv) If the value of firstName is Freddy, print the message No. Otherwise print the value of x.
Answer:
pi = 22.0 / 7;
Problem 119 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;
}
Error -20 5 4
Aha!
Aha!
Error 10 10
***
**
*
***
**
*
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 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 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
Your firstName is x
Answer:
(iii) Read in the values of firstName and lastName (in this order).
Answer:
(iv) If the value of firstName is not Freddy, print the message Hello. Otherwise end the program.
Answer:
(v) Prompt the user to enter values for x, y and z and read their response to appropriate variables.
Answer:
Problem 123 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;
}
Illegal Goodbye!
Illegal -144 12
12
144 12
Problem 124 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 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 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
x + y + z is positive
Answer:
(iv) If the value of firstName is Freddy, print the value of pi. Otherwise print Hello.
Answer:
Problem 127 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;
}
Error 6 5
Aha!
Aha!
Error -2 11
-11
Problem 128 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 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 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
PI is 22/7
Answer:
(ii) Print to the screen the value of x/y + z (use integer division).
Answer:
(iv) If the value of firstName is Freddy, exit the program. Otherwise print Hello.
Answer:
(v) Prompt the user to enter a last name and first name and read their response to appropriate variables.
Answer:
Problem 131 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;
}
Illegal -20
Goodbye!
22 2
22
4 2
Problem 132 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 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 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 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 declarations have been made and
the declared variables have been set to have legal values.
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:
pi = 22 / 7.0;
(v) If the value of firstName is not Freddy, print the message No. Otherwise print the value of x.
Answer:
Problem 135 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;
}
Aha!
Error -20 5 4
Problem 136 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 137 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 integer n between 20 and 40: ";
cin >> n;
if (n < 20 || n > 40) return 0;
for (int i = 1; i <= n/5; i++)
if (i % 2 == 1) cout << i << 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 declarations have been made and
the declared variables have been set to have legal values.
Your name is x
Answer:
(ii) Print to the screen the quotient when x is divided by yz (use integer division).
Answer:
(iii) Read in the values of firstName and lastName (in this order).
Answer:
(iv) If the value of x is 5 or firstName is not Freddy, print the message Hello. Otherwise end the program.
Answer:
(v) Prompt the user to enter values for x, y and z and read their response for x only.
Answer:
Problem 139 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;
}
Illegal Goodbye!
Goodbye!
144 12
12
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.
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 141 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 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. Assume that the following declarations have been made and
the declared variables have been set to have legal values.
x % y + z is positive
Answer:
(iv) If the value of firstName is Freddy and y is equal to z, print the value of pi. Otherwise print No.
Answer:
Problem 143 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;
}
Aha!
Error 6 5
-11
(d) The user enters: 3 11
Error -3 11
Problem 144 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 145 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 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.
(i) Print to the screen the message:
2 + 2 = 3
Answer:
(iii) Print a random number r with 7 ≤ r ≤ 27. (An appropriate C++ function must be used to make the random
number.)
Answer:
(iv) Ask the user to enter their age. If their answer does not satisfy 5 ≤ age ≤ 99 exit the program immediately.
Answer:
(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:
#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;
}
Illegal
111
10
12345678910
Problem 148 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:
Answer:
#include <iostream>
using namespace std;
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 149 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 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.
(i) Print to the screen the message:
1 x 2 x 3 x 4 = 24
Answer:
(ii) Print a random number r with 11 ≤ r ≤ 29. (An appropriate C++ function must be used to make the random
number.)
Answer:
(iv) Ask the user to enter their age. If their answer does not satisfy 0 ≤ age ≤ 1000 exit the program immediately.
Answer:
(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:
Problem 151 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;
}
Illegal
110
1234567891
Problem 152 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:
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 153 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 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.
(i) Print to the screen the message:
Hello
Hello Hello
Answer:
(iii) Print a random number r with −7 ≤ r ≤ 0. (An appropriate C++ function must be used to make the random
number.)
Answer:
(iv) Ask the user to enter their age. If their answer does not satisfy 5 ≤ age ≤ 99 print the word “Illegal”.
Answer:
(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:
Problem 155 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;
}
Illegal
121
101214161820
13579111315171921
Problem 156 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:
Answer:
#include <iostream>
using namespace std;
int main() {
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 157 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 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.
(i) Print to the screen the message:
2 x 2 = 22
Answer:
(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:
(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:
Problem 159 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;
}
Illegal
1111120
1021
111111111121
Problem 160 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:
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 161 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;
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.
(i) Print to the screen the message:
007 = 7
Answer:
(ii) Print the numbers from 1 to 1000 to the screen, one number per line.
Answer:
(iii) Print the numbers from 1 to 1000 to the screen, ten numbers per line.
Answer:
(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:
Problem 163 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;
}
Illegal
55
55
Illegal
123121567
567565567
Problem 164 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: 89
Sum of digits: 17
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a 2 digit integer: ";
cin >> n;
Problem 165 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;
return 0;
}
Problem 166 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 167 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
Illegal
Illegal
123121567
Illegal
Problem 168 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter two different 2 digit integers: ";
cin >> n >> m;
Problem 169 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;
return 0;
}
Problem 170 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:
(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:
(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:
Problem 171 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;
}
Illegal
65
5
Illegal
123567
568567
Problem 172 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: 79
Absolute difference: 2
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a 2 digit integer: ";
cin >> n;
Problem 173 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;
return 0;
}
Problem 174 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:
(ii) Print the even numbers from -200 to 200 to the screen, one number per line.
Answer:
(iii) Print the even numbers from -200 to 200 to the screen, three numbers per line.
Answer:
(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:
Problem 175 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;
}
56
6
Illegal
Illegal
123567
Illegal
Problem 176 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter two different 2 digit integers: ";
cin >> n >> m;
********
********
********
********
Answer:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) return 0;
Problem 178 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;
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:
Problem 179 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;
}
15
16
Problem 180 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:
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 181 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 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. Assume that the following variables have been declared, and
if necessary have values, for each part:
int number;
double x, y;
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:
Problem 183 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;
}
CSCI 111
n = 111
111, 101, 91, 81, 71, 61, 51, 41, 31, 21,
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) 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 185 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:
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 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.
(a) Print to the screen the message:
2 + 2 = 5
(b) Print all the odd numbers from 1 to 1000 to the screen (one number per line).
(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.
(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 187 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;
You rat!
Goodbye Kamil!
Hello Carl you are about 200
Problem 188 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 189 Write a complete C++ program that asks a user to enter their day and month of birth. If the
user’s 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:
#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 190 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
(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;
(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.
Problem 191 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;
Negative
-10
7-10
710
Problem 192 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;
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 194 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
(b) Print all the numbers from 1 to 1000 to the screen (one number per line).
(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:
(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 195 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;
Freddy
Negative
Problem 196 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 the sum is 1
2 3 4 the sum is 9
3 4 5 6 7 8 9 the sum is 42
4 5 6 7 8 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 197 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:
Answer:
#include <iostream>
using namespace std;
int main () {
int q, d, n, p;
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.
(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:
(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 199 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;
}
Freddy
XX
33
1111008978675645342312
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 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;
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 201 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 user’s age.
Here is an example of how the program should work:
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 202 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:
(c) Ask the user to type a password and then to type it again. Print Error if the two words are different.
Answer:
(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:
(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 203 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;
*+*+
*+*+
*+*+*+*+*+*+*+*+*+*+
*+*+*+*+*+*+*+*+*+*+
*+*+*+
Problem 204 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);
return 0;
}
Problem 205 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:
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 206 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:
(b) Print (to the output screen) the square root of 19683:
Answer:
(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:
(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:
(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 207 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;
Freddy
007007
108
99
Problem 208 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 the sum is 1
1 2 the sum is 3
1 2 3 the sum is 6
1 2 3 4 the sum is 10
1 2 3 4 5 the sum is 15
1 2 3 4 5 6 the sum is 21
1 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);
return 0;
}
Problem 209 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:
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 210 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Please enter a positive integer: ";
cin >> n;
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;
}
No good!
Odd
121
Odd
1
Odd
Problem 211 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:
(c) Make the user enter 6 decimal values and print their product.
(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.)
(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.).
Problem 212 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 even integer: ";
cin >> n;
while ((n <= 0) || (n % 2 != 0)) {
cout << "Illegal. Try again: ";
cin >> n;
}
for (r = 1; r < n; r++) {
cout << "*";
for (c = 2; c < n; c++) cout << " ";
cout << "*" << endl;
}
for (c = 1; c <= n; c++) cout << "*";
cout << endl;
return 0;
}
Problem 213 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:
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 214 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Please enter an integer: ";
cin >> n;
Even
Odd
Goodbye
Hello
Goodbye
Goodbye
Even
Problem 215 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
(b) Print (to the output screen) a message made from the first 20 integers:
1234567891011121314151617181920
(c) Make the user enter 6 decimal values and print their sum.
(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.)
(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. ).
*******
*
*
*
*
*
*
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 217 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:
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: " << sqrt(x) << endl;
return 0;
}
Problem 218 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
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;
}
Illegal
11.0
111.1
441.442.443.444.4
Problem 219 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:
(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:
(e) Print the value of a randomly selected two digit integer. (The program should make a random selection using
the function rand).
Answer:
Problem 220 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;
}
Problem 221 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:
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:
(d) Make the user enter 10 integer values and print the sum.
Answer:
Problem 222 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:
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 223 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:
(c)
Answer:
int adjust(int a)
(d)
Answer:
(e)
Answer:
Problem 224 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 225 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:
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 226 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:
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:
(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 227 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#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!
25*
fun3 1
20* 2
Problem 228
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 229 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#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;
}
Problem 230 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:
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:
(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:
Problem 231 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:
Answer:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive number: ";
cin >> x;
if (x <= 0) exit(1);
Problem 232
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;
}
Problem 233 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 user’s 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 234
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 235 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#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;
}
Problem 236 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: " << endl;
cin >> x >> y >> z;
if ( (x > y && y > z) || (z > y && y > x)) cout << y;
if ( (y > z && z > x) || (x > z && z > y)) cout << z;
if ( (z > x && x > y) || (y > x && x > z)) cout << x;
cout << endl;
return 0;
}
Problem 237 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;
}
Problem 238 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;
}
Problem 239 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:
(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 240 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
#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;
}
Illegal
4!4!4!4!4
Problem 241 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 242 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:
(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. Give another: ";
cin >> name;
}
Problem 243 Consider the following C++ program. Explain what output is produced in response to the given
user inputs.
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;
}
Problem 244 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;
}
Problem 245 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:
(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;
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:
Problem 246 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:
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;
Problem 247
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;
}