8/11/2019 Problem Exercises Chapter 01
1/12
1.
Page 55: Exercise 23. Write a program that asks the user to enter two numbers
and prints the sum, product, difference, and quotient of the two numbers.
#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain()
{intnum1, num2; // declare variables
cout > num1 >> num2; // read values from keyboard
// output the results
cout
8/11/2019 Problem Exercises Chapter 01
2/12
2.
Page 55: Exercise 24. Write a program that prints the numbers 1 to 4 on the
same line with each pair of adjacent numbers separated by one space. Write
the program in each of the following ways:a)Using one output statement with one stream insertion operator.
b)Using one output statement with four stream insertion operators.
c)Using four output statements.
#includeusingstd::cout;
usingstd::endl;
intmain (){
// Part A: Using one output statement// with one stream insertion operatorcout
8/11/2019 Problem Exercises Chapter 01
3/12
3.
Page 55: Exercise 25. Write a program that asks the user to enter two integers,
and then prints the larger number followed by the words "is larger." If the
numbers are equal, print the message "These numbers are equal."
#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain(){
intnum1, num2; // declare variables
cout > num1 >> num2; // read values from keyboard
// Compares if values of "num1" and "num2" are equalif( num1 == num2 ) // If condition is true, then
cout
8/11/2019 Problem Exercises Chapter 01
4/12
4.
Page 55: Exercise 26. Write a program that inputs three integers from the
keyboard and prints the sum, average, product, smallest and largest of these
numbers.#includeusingstd::cout;usingstd::cin;usingstd::endl;
intmain() {intnum1,num2,num3,smallest,largest; //declare variables
cout > num1 >> num2 >> num3; //read values from keyboard
largest = num1; // assume "num1" is the largestif( num2 > largest ) //is "num2" larger than "largest"?
largest = num2; // then "num2" is the largest
if( num3 > largest ) //is "num3" larger than "largest"?largest = num3; // then "num3" is the largest
smallest = num1; // assume "num1" is the smallestif( num2 < smallest ) //is "num2" smaller than smallest?
smallest = num2; // then "num2" is the smallestif( num3 < smallest ) //is "num3" smaller than smallest?
smallest = num3; // then "num3" is the smallest
// output the resultscout
8/11/2019 Problem Exercises Chapter 01
5/12
5.
Page 56: Exercise 28. Write a program that prints a box, an oval, an arrow and
a diamond.
#includeusingstd::cout;usingstd::endl;
intmain(){
// output the resultscout
8/11/2019 Problem Exercises Chapter 01
6/12
6.
Page 56: Exercise 30. Write a program that reads in five integers from the
keyboard and determines and prints the largest and the smallest integers in the
group. Use only the programming techniques you learned in this chapter.#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain(){
// declare variablesintnum1, num2, num3, num4, num5, largest, smallest;
cout > num1 >> num2 >> num3 >> num4 >> num5; // input
largest = num1; // assume that "num1" is the largest
smallest = num1; // assume that "num1" is the smallest
if( num1 > largest ) //is "num1" larger than "largest"?largest = num1; // then "num1" is the largest
if( num2 > largest ) //is "num2" larger than "largest"?largest = num2; // then "num2" is the largest
if( num3 > largest )//
largest = num3;if( num4 > largest )
largest = num4;if( num5 > largest )
largest = num5;
if( num1 < smallest )//is "num1" smaller than smallest?
smallest = num1; // then "num1" is the smallestif( num2 < smallest )//is "num2" smaller than smallest?
smallest = num2; // then "num2" is the smallestif( num3 < smallest )//
smallest = num3;if( num4 < smallest )
smallest = num4;
if( num5 < smallest )smallest = num5;
8/11/2019 Problem Exercises Chapter 01
7/12
// output the resultscout
8/11/2019 Problem Exercises Chapter 01
8/12
7.
Page 56: Exercise 31. Write a program that reads an integer and determines
and prints whether it is odd or even. (Hint: Use the modulus operator. An even
number is a multiple of two. Any multiple of two leaves a remainder of zerowhen divided by 2.)
#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain(){
intnum; // declare variable
cout > num; // read value from keyboard
if( (num % 2) == 0 ) // If "num" is even, thencout
8/11/2019 Problem Exercises Chapter 01
9/12
8.
Page 56: Exercise 32. Write a program that reads in two integers and
determines and prints if the first is a multiple of the second. (Hint: Use the
modulus operator.)
#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain(){intnum1, num2; // declare variables
cout > num1 >> num2; // read values from keyboard
// If "num1" is a multiple of "num2", thenif( (num1 % num2) == 0 )
cout
8/11/2019 Problem Exercises Chapter 01
10/12
9.
Page 57: Exercise 36. Write a program that inputs a five-digit number numand
prints nums digits separated from one another by three spaces each. For
example, if the user types in 42339 the program should print: 4 2 3 3 9Hint: Use the integer division and modulus operators.
#includeusingstd::cout;usingstd::endl;usingstd::cin;
intmain(){
intnum; // declare variable
cout > num; // read values from keyboard
cout
8/11/2019 Problem Exercises Chapter 01
11/12
10.
Page 57: Exercise 37. Using only the techniques you learned in this chapter,
write a program that calculates the squares and cubes of the numbers from 0 to
10 and uses tabs to print the following table of values:number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 1256 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
#includeusingstd::cout;usingstd::endl;
intmain(){
intnum; // declare variable
num = 0; // Initialize "num" to value 0cout
8/11/2019 Problem Exercises Chapter 01
12/12
num = num + 1;cout
Top Related