factorial c program using for loop 1

factorial c program using for loop

#include
int main(){
  int i,f=1,num;
 
  printf("Enter a number: ");
  scanf("%d",&num);
 
  for(i=1;i<=num;i++)
      f=f*i;
 
  printf("Factorial of %d is: %d",num,f);
  return 0;
}

Here is what the above code is Doing:
1. First, we have declared a variable f and initialized it with 1.
2. Then, we have declared a variable num and initialized it with the number whose factorial we want to find.
3. Then, we have declared a variable i and initialized it with 1.
4. Then, we have used a for loop to calculate the factorial of the number.
5. Inside the for loop, we have multiplied the value of f with i and assigned it to f.
6. After the for loop, we have printed the factorial of the number.

Similar Posts