B option: (The best grade is a 90%) Write a program that will calculate the fact
ID: 3641142 • Letter: B
Question
B option: (The best grade is a 90%)Write a program that will calculate the factorial of number.
Example: the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120
? Input the starting number in main()
? Pass the number to a function called factorial() to calculate the Factorial number
Prototype: double factorial (double num);
In the function:
? Use a for loop to calculate the answer
? Use a return statement to return the answer back to the caller.
? Back in main, ask the user if they want to another enter number. (Use a loop in main())
A option: (The best grade is a 100%)
Create a function called yesno() Prototype: int yesno();
? yesno() should have a while loop in it.
? Keep looping until they enter y, Y, n, or N.
? If the user enters y, or Y return 1.
? If the user enters n or N return 0.
? Call this function when you need a yes or a no response.
Example of using yesno() from main()
do {
// lots of code
} while (yesno() = = 1);
Explanation / Answer
#include <iostream>
using namespace std;
int yesno() {
bool stop = false;
char input;
while(!stop) {
cin >> input;
if(input == 'y' || input == 'Y' || input == 'n' || input == 'N') {
stop = true;
}
}
if(input == 'y' || input == 'Y') {
return 1;
}
else {
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.