C program to find factorial of a number

  • Post last modified:May 12, 2022
  • Reading time:2 mins read
  • Post category:Programming

Factorial of Number :

Factorial of a whole number ‘n’ is defined as the product of that number with every whole number till 1. For example, the factorial of 4 is 4×3×2×1, which is equal to 24. It is represented using the symbol ‘!’ So, 24 is the value of 4!

C program to find factorial of a number

1. Using for loop

//program by xnetking
#include<stdio.h>
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;
} 

2. Using functions

#include<stdio.h>
#include<conio.h>
void main()
{
int no,factorial;
int fact (int no);
clrscr();
printf("Enter a number:");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial=%d",factorial);
getch();
 }
int fact (int no)
{
int i,ans;
for(i=1,ans=1;i<=no;i++)
{
    ans=ans*i;
}
return ans;
}

3. Using while loop

#include <stdio.h>
int main()
{
    int n,i,f;
    f=i=1;
    printf("Enter a Number to Find Factorial: ");
    scanf("%d",&n);
    while(i<=n)
    {
        f*=i;
        i++;
    }
    printf("The Factorial of %d is : %d",n,f);
    return 0;
}

keywords: c program factorial of given number

Leave a Reply