Counting Change Write a C++ program that performs the following task. A user is
ID: 641949 • Letter: C
Question
Counting Change
Write a C++ program that performs the following task. A user is to be prompted for an integer value sum that is supposed to be in the interval 0 . . 99. If the value of sum is not in the proper interval, the only actions that should take place are displaying an error message and returning control back to the operating system.
If the value for sum is in the proper interval, the user should be prompted for a number of dimes d, a number of nickels n, and a number of pennies p. If the monetary value of the coins provided by the user equals sum, display the message "Yes" and exit the program. Otherwise display the message "No" and prompt the user one more time to enter a correct amount of coins. Name this program CountingChange.cpp
(Use a function to prompt the user for the coins and return the monetary value of the coins.)
NOTE: The program standards are given below:
1. Program Header . In addition to you name and project number, include a brief description of the program functionality in the header comment for the program
2. Variables and Constants. All variables and constants in a program must be declared at the beginning of the program
3. Initialization. Variables may not be initialized in their declaration.
4. Printing of if-then. Use one of the following
if (expression) statement
if (expression)
Single-statement
if (expression) {
Multiple-statements
}
5. Printing of if-then-else. Use one of the following
if (expression) statement
else statement
if (expression)
Single-statement
else
Single-statement
if (expression) {
Multiple-statements
} else {
Multiple-statements
}
6. Printing of while loops. Use one of the following
while (expression) statement
while (expression)
Single-statement
while (expression) {
Multiple-statements
}
7. For loops. The bodies of all for-loops must be indented at least 3 (but no more than 5) spaces:
for(int i = 0; i < n; i++) {
cout << "Enter the number:" << flush;
cin >> num;
if (num < 0) num_neg++;
else if (num > 0) num_pos++;
else num_zero++;
}
8. Methods. The bodies of all functions must be indented at least 3 (but no more than 5) spaces:
double CircleArea(double r) {
const double Pi = 3.1415;
return Pi * r * r;
}
Explanation / Answer
#include <iostream>
using namespace std;
int calMoney()
{
int n, d, p;
cout << "Enter dime: ";
cin >> d;
cout << "Enter nickle: ";
cin >> n;
cout << "Enter penny: ";
cin >> p;
return (d * 10 + 5 * n + p);
}
int main()
{
int num, sum, total;
cout << "Enter a number: ";
cin >> num;
sum = 0;
for (int i = 1; i <= num; i++)
sum = sum + i;
if ((sum < 0) || (sum > 99))
cout << "Not in 0 to 99 interval" << endl;
else
{
for (int j = 0; j < 2; j++)
{
total = calMoney();
if (total == sum)
{
cout << "Yes";
break;
}
}
}
return 0;
}