Thursday, 9 June 2016

Factorial of a number

Factorial of an integer is product of the number and all integers below it until 1.
5! = 5 * 4! 
   = 5 * 4 * 3!
   = 5 * 4 * 3 * 2!
   = 5 * 4 * 3 * 2 * 1!
   = 5 * 4 * 3 * 2 * 1

Code

#include<stdio.h>

int main()
{
    int fact=1,n,i;

    printf("Enter a number ");
    scanf("%d",&n);

    for(i=n;i>=1;i--)
        fact = fact * i;

    printf("\nFactorial of %d = %d",n,fact);

    return 0;
}

O/P



No comments:

Post a Comment