Enter a positive integer between 0 and 9: -6
Input number cannot be less than 0.
Enter a positive integer between 0 and 9: 12 Input number cannot be greater than 9.
Enter a positive integer between 0 and 9: 6 0! = 1
1! = 1
2! = 2
3! = 6
4! = 24 5! = 120 6! = 720
Enter a positive integer between 0 and 9: 12 Input number cannot be greater than 9.
Enter a positive integer between 0 and 9: 6 0! = 1
1! = 1
2! = 2
3! = 6
4! = 24 5! = 120 6! = 720
c) Use a structure that corresponds to the for statement of C when calculating n!.
-
d) For simplicity, it is assumed that the user input is always a whole number, and
therefore this is no need to check.
-
e) Save the flowchart to a file named “factorial.rap”.
-
f) Demonstrate the factorial.rap file to GA/TAs and run with different input
values.
int main(void)
{
//Variable Declaration
int n,t=0,result=1;
//Input from the user
printf("Enter a positive integer between 0 and 9:");
scanf("%d",&n);
if (n<0) {//If the input number is less than 0
printf("Input number cannot be less than 0.\n");}
else if (n>9){ //If the input number is greater than 9
printf("Input number cannot be greater than 9.\n");
}
else{
printf("%d! = %d\n",t,result); //To print 0! = 1
for(t = 1; t <= n; t++){
result=result*t;
printf("%d! = %d\n",t,result);}
}
return 0; }
Comments
Post a Comment