0% found this document useful (0 votes)
18 views13 pages

Problem Exercises Chapter 4 and 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Problem Exercises Chapter 4 and 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Problem Exercices 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 << "Enter first number: "; // prompt user


cin >> largest; //read "largest" from user

while (++counter < 5) { // loop 10 times


cout << "Enter next number: "; // prompt user
cin >> number; // read "number" from user

if (number > largest) { // if value of "number" > "largest"


largest = number; // then update value of "largest"
}
}

cout << "Largest is: " << largest << endl; // output result

return 0;
}

Sample Output:
Problem Exercices Chapter 4 and 5
Page 2

2. The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”)


and is defined as follows:
n! = n * (n-1) * (n-2) * ... * 1 where n! = 1 for n = 0
For example 5! = 5 * 4 * 3 * 2 * 1 which is 120
Write a program that reads a nonnegative integer and computes and prints its
factorial.

#include <iostream>
using namespace::std;

int main()
{
int n = 0, num;
unsigned factorial = 1;

do { // "do/while" to insure that the user enters a positive integer


cout << "Enter positive integer: "; // prompt user
cin >> num; // read "num" from user
} while(num < 0); // while "num" is negative, loop again

// while loop to count from "n" to value of "num-1".


// First it tests if "n" is < than "num", then it increments "n" by 1
while(n++ < num) {
factorial = factorial * n; // calculate "factorial"
}

// 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;

// prints header of the table


cout << "Number \t Factorial of number \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

// inner while loop to count from 1 to the value of "num",


// and calculate the "factorial" of "num"
while(n++ < num) {
factorial = factorial * n; // calculate "factorial"
}

// 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 header of the table


cout << "N" << "\t" << "10*N" << "\t" << "100*N"
<< "\t" << "1000*N" << "\n";

// while loop to count from 1 to 5, in each iteration it prints


// the multiples of 10, 100, and 1000 of the value of "n".
// First it increments "n" by 1, then it tests if "n" is <= 5.
while(++n <= 5) {

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

5. An integer is said to be prime if it is divisible by only 1 and itself. For example


2, 3, 5, and 7 are primes, but 1, 4, 6, 8, and 9 are not.
Write a program that determines whether or not a number is a prime.

#include <iostream>
using namespace::std;

int main()
{
int Num, Flag = 0; // declare variable, & initialize "Flag" to 0

cout << "Enter a number: "; // prompt user


cin >> Num; // read "Num" from user

if (Num <= 1) { // if Num<=1, then it is not a prime


cout << Num << " is not a prime" << endl;
}

else { // if no, then determine whether "Num" is a prime or not


// 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"
// if yes, then output that "Num" is not a prime
cout << Num << " is not a prime" <<endl;
Flag = 1; // set "Flag" to 1
break; // exit for loop
}
}

if (Flag == 0) { // if "Flag" is still = 0,


// then output that "Num" is a prime
cout << Num << " is a prime" << endl;
}
}

return 0;
}
Problem Exercices Chapter 4 and 5
Page 6

Two sample Outputs:


Problem Exercices Chapter 4 and 5
Page 7

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 << "Enter a positive number: "; // prompt user


cin >> Val; // read "Val" from user

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

cout << "On the ";

switch (day) { // switch for current day


case 1: // if "day" is equal to 1, then
cout << "first"; // output "first" to the screen
break; // break the "switch" structure
case 2:
cout << "second";
break;
case 3:
cout << "third";
break;
case 4:
cout << "fourth";
break;
case 5:
cout << "fifth";
break;
case 6:
cout << "sixth";
break;
case 7:
cout << "seventh";
break;
Problem Exercices Chapter 4 and 5
Page 9

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

cout << " day of Christmas,\n";


cout << " My true love sent to me:\n";

switch (day) { // switch for gifts


case 12: // if "day" is equal to 12, then
// display "Twelve drummers drumming", but don’t exit "switch"
cout << "\t Twelve drummers drumming, \n";
case 11:
cout << "\t Eleven pipers piping, \n";
case 10:
cout << "\t Ten lords a-leaping, \n";
case 9:
cout << "\t Nine ladies dancing, \n";
case 8:
cout << "\t Eight maids a-milking, \n";
case 7:
cout << "\t Seven swans a-swimming, \n";
case 6:
cout << "\t Six geese a-laying, \n";
Problem Exercices Chapter 4 and 5
Page 10

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

cout << "Enter the square side: "; // prompt user


cin >> side; //read "side" from user

size = side; // initialize "size" to "side"


while(side > 0) {
rowPosition = size;
while(rowPosition > 0) {
if(size == side || side == 1 ||
rowPosition == 1 || rowPosition == size) {
cout << "* ";
} // end if
else {
cout << " ";
} // end else
--rowPosition;
} // end while
cout << '\n';
--side;
} // end while
return 0;
}
Problem Exercices Chapter 4 and 5
Page 12

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';
}

// bottom half of the diamond


for(int row = 4; row >= 1; --row) {
for(int space = 1; space <= 5 - row; ++space)
cout << ' ';
for(int ast = 1; ast <= 2 * row - 1; ++ast)
cout << '*';
cout << '\n';
}
return 0;
}

Sample Output:

You might also like