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
#include <iostream>
using namespace std;
int fact(int num){
int ans = 1;
for(int i = 1; i <= num;i++){
ans *= i;
}
return ans;
}
int main(int argc, char *argv[]){
int a;
cin >> a;
if (a >= 2 && a <= 10){
}else{
return -1;
}
cout << fact(a);
}
Comments
Leave a comment