C Programming - Calculating factorial values Source Code



  1. Start RAPTOR and create a flowchart that asks a user for a number (n) between
    0 and 9 and computes and displays the factorial table from 0 to n. A sample interaction is shown below:
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
c) Use a structure that corresponds to the for statement of C when calculating n!.
  1. d)  For simplicity, it is assumed that the user input is always a whole number, and
    therefore this is no need to check.
  2. e)  Save the flowchart to a file named “factorial.rap”.
  3. f)  Demonstrate the factorial.rap file to GA/TAs and run with different input
    values. 
#include <stdio.h>
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