In: Computer Science
Write a C program that evaluates the factorials from 1 to 5. Print results in a tabular format.
CODE:
#include <stdio.h>
int main()
{
int fact=1;
printf("i |\t Factorial\n");
printf("----|--------------\n");
for(int i=1;i<=5;i++)
{
switch(i){
case 1: fact=fact*i;
printf("%d |\t %d\n",i,fact);
break;
case 2: fact=fact*i;
printf("%d |\t %d\n",i,fact);
break;
case 3: fact=fact*i;
printf("%d |\t %d\n",i,fact);
break;
case 4: fact=fact*i;
printf("%d |\t %d\n",i,fact);
break;
case 5: fact=fact*i;
printf("%d |\t %d\n",i,fact);
break;
default:break;
}
}
return 0;
}
SCREENSHOT AND EXPLANATION:
OUTPUT: