C program to print factorial of a number

Before write a C program to print factorial of a number, first try to understand what is factorial. Factorial is product of all positive integers in descending orders. Suppose we want to know the factorial of 6.
6! = 6*5*4*3*2*1 = 720
So formula is = n! = n*(n-1)!

C program to print factorial of a number using for loop – source code

/*************************************
C program to print factorial of a number

https://bptutorials.com

***************************************/
#include <stdio.h>
#include <conio.h>

void main ()
{
  int factorial = 1, i, num;
  printf ("Enter Number : ");
  scanf ("%d", &num);
  for (i = 1; i <= num; i++)
    {
      factorial = factorial * i;
    }
  printf ("Factorial of %d is %d", num, factorial);

  getch ();
}

Output :

factorial of a number in c using for loop
factorial program in c

Factorial program in C using while loop

/***********************************************

https://bptutorials.comp

 factorial program in C using while loop

************************************************/
#include <stdio.h>


void main()
{
    int num, fact = 1, i = 1;
    printf("Enter a Number : ");
    scanf("%d", &num);
    while(i<=num)
    {
        fact = fact*i;
        i++;
    }
    printf("Factorial of %d is : %d ", num, fact);

   
}

Output :

Factorial program in C using while loop
Share This!

Leave a Reply

Your email address will not be published. Required fields are marked *