Homework 8 Solution
1. Write a program that reads in a product computation of the following
form:
N0 * N1 * N2
* N3 * … * Nn =
... and returns the product of the given ints,
along with the number of terms in the product. Note that you don’t know how
many integer values the user will enter until they enter an equal sign (=).
An example run of your program might look like this (with the user input in
red):
Please enter a product to be computed, followed by an = sign. : 4 * 56 * 2 * 7 * 4 = The product of the 5 numbers entered is 12544 |
Turn In a printout of the source code for your program and two
screenshots, one showing your program running with the input from above, and
the second screen shot showing your program running for the below input:
111 * 222 * 333 * 444 * 555 * 666 = |
2. Explain the strange answer you get for the second input above (111 * 222
* …)
Turn in your source code, the two screen shots, and the answer to
question 2.
Solution
// KGS
// Hmwk 8
// This prints the products of a series of integers
#include <iostream>
using namespace std;
int main (){
int num;
int product = 1;
int count = 0;
char junk;
cout << "This program was written by KGS." << endl;
cout << "Please enter a product to be computed, followed by an =
sign." << endl;
cout << ": ";
cin >> num >> junk;
while (junk != '='){
count++;
product = product * num;
cin
>> num >> junk;
}
count++;
product = product * num;
cout << "The product of the " << count <<
" numbers entered is " <<
product << "." << endl;
return 0;
}