LOOPING STATEMENTS AND FUNCTIONS
Create a flowchart to guide you in the process
Create a function that will accept the value of i and return the value of n
Possible values of i is any positive value from 2 to 10
n is computed based on the value of i; see the following table for the sample Input / Output
i Process n
3 1*2*3 6
5 1*2*3*4*5 120
7 1*2*3*4*5*6*7 5040
Screen/Layout
(Home Screen)
Input i:3
Process: 1*2*3
Output: 6
Try Another [Y/N]: Y
Input i: 5
Process: 1*2*3*4*5
Output: 120
Try Another [Y/N]: Y
#include <iostream>
using namespace std;
int foo(int k) {
int ans = 1;
while (k--) {
ans *= k;
}
return ans;
}
int main()
{
while (true) {
int n;
char q;
cout << "Input i: ";
cin >> n;
cout << "Output: " << foo(n) << '\n';
cout << "Try Another [Y/N]: ";
}
return 0;
}
Comments
Leave a comment