Problem Exercises Chapter 4 and 5
Problem Exercises Chapter 4 and 5
Page 1
Problem Exercises
1. Write a C++ program that inputs a series of 5 numbers, and determines and
prints the largest of these numbers.
#include <iostream>
using namespace::std;
int main()
{
int counter = 0, number, largest; // declare variables
cout << "Largest is: " << largest << endl; // output result
return 0;
}
Sample Output:
Problem Exercices Chapter 4 and 5
Page 2
#include <iostream>
using namespace::std;
int main()
{
int n = 0, num;
unsigned factorial = 1;
// output result
cout << num << "! is " << factorial << endl;
return 0;
}
Sample Output:
Problem Exercices Chapter 4 and 5
Page 3
3. Write a program that evaluates the factorials of the integers from 1 to 5. Print
the results in tabular format.
#include <iostream>
using namespace::std;
int main()
{
int factorial, n;
// outer for loop to count from 1 to 5, and for each value of "num"
// calculate the value of "factorial"
for (int num = 1; num <= 5; ++num) {
n = 0; // reinitialize "n" to 0
factorial = 1; // reinitialize "factorial" to 1
// output results
cout << num << '\t' << factorial << '\n';
}
return 0;
}
Sample output:
Problem Exercices Chapter 4 and 5
Page 4
4. Write a C++ program that utilizes looping and the tab escape "\t" to print the
following table of values:
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
#include <iostream>
using namespace::std;
int main()
{
int n = 0; // declare and initialize "n" to 0
// prints the multiples of 10, 100, and 1000 of the value "n"
cout << n << '\t' << 10*n << '\t' << 100*n
<< '\t' << 1000*n << '\n';
}
return 0;
}
Sample Output:
Problem Exercices Chapter 4 and 5
Page 5
#include <iostream>
using namespace::std;
int main()
{
int Num, Flag = 0; // declare variable, & initialize "Flag" to 0
return 0;
}
Problem Exercices Chapter 4 and 5
Page 6
6. Write a program that reads an integer from the keyboard then determines all
the prime numbers between 2 and that integer.
#include <iostream>
using namespace::std;
int main()
{
int Val, Flag = 0; // declare variable, & initialize "Flag" to 0
cout << "Prime numbers from 1 to " << Val << " are: \n";
// loop from 2 to "Val", & for each iteration test if "Num" is a prime
for (int Num = 2; Num <= Val; Num++) {
Flag = 0; // for each iteration reset "Flag" to 0
// loop from 2 to "Num", & for each iteration test if "Num" is dividable by "i"
for (int i = 2; i < Num; i++) {
if ((Num % i) == 0) { // test if "Num" is dividable by "i"
Flag = 1; // if yes, then set "Flag" to 1
}
} // end for
if (Flag == 0) { // test if "Flag" is still = 0
cout << Num << endl; // if yes, then output "Num"
}
} // end for
return 0;
}
Sample Output:
Problem Exercices Chapter 4 and 5
Page 8
7. Write a program that uses repetition and switch structures to print the song
“The Twelve Days of Christmas.” One switch structure should be used to print
the day (i.e., “First,” “Second,” etc.). A separate switch structure should be
used to print the remainder of each verse.
#include <iostream>
using namespace::std;
int main()
{
for (int day = 1; day < 13; day++) { // loop 12 times
case 8:
cout << "eighth";
break;
case 9:
cout << "ninth";
break;
case 10:
cout << "tenth";
break;
case 11:
cout << "eleventh";
break;
case 12:
cout << "twelfth";
break;
} // end switch for current day
case 5:
cout << "\t Five golden rings, \n";
case 4:
cout << "\t Four calling birds, \n";
case 3:
cout << "\t Three French hens, \n";
case 2:
cout << "\t Two turtle doves, and \n";
case 1:
cout << "A partridge in a pear tree.\n\n";
} // end switch for gifts
} // end loop
return 0;
}
Sample Output:
Problem Exercices Chapter 4 and 5
Page 11
8. Write a program that reads in the size of the side of a square and then prints a
hollow square of that size out of asterisks and blanks. Your program should
work for squares of all side sizes between 1 and 20. For example, if your
program reads a size of 4, it should print:
* * * *
* *
* *
* * * *
#include <iostream>
using namespace::std;
int main()
{
int side, rowPosition, size; // declare variables
Sample Output:
Problem Exercices Chapter 4 and 5
Page 13
9. Write a program that prints a diamond shape. You may use output statements
that print either a single asterisk (*) or a single blank. Maximize your use of
repetition (with nested for structures) and minimize the number of output
statements.
#include <iostream>
using namespace::std;
int main()
{
// top half of the diamond
for(int row = 1; row <= 5; ++row) {
for(int space = 1; space <= 5 - row; ++space)
cout << ' ';
for(int ast = 1; ast <= 2 * row - 1; ++ast)
cout << '*';
cout << '\n';
}
Sample Output: