C Program To Find Factorial of a Number with Example

Logic to write C program to find factorial of a number

  • For '1', the value of the fact variable will be fact = fact*1 i.e. fact = 1*1 and it becomes face = 1;
  • For '2', the value of the fact variable will be fact = fact*2 i.e. fact = 1*2 and it becomes fact = 2;
  • For '3', the value of the fact variable will be fact = fact*3 i.e. fact = 2*3 and it becomes fact = 6;
  • For '4', the value of the fact variable will be fact = fact*4 i.e. fact = 6*4 and it becomes fact = 24;
  • For '5', the value of the fact variable will be fact = fact*5 i.e. fact = 24*5 and it becomes fact = 120;
#include <stdio.h> 
main()
{
int i, n, fact;
printf("\n Enter the number : ");
scanf("%d",&n); fact = 1;
//Loop to generate the factorial number
for(i=1;i<=n;i++)
{
fact = fact*i;
}
printf("\n The factorial of given number is: %d",fact);
}

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.