Wednesday, January 1, 2014

Factorial

Factorial of a number 'n', denoted by n!, is the product of non negative number less than or equal to the given number 'n'.
n! = n*(n-1)!
n! = n*(n-1)*(n-2)*.....*(n-n)!
n! = n*(n-1)*(n-2)*.....*(0!)
n! = 0?
Nop
0! = 1
n! = n*(n-1)*(n-2)*....*(n-(n-1))*1
n! = n*(n-1)*(n-2)*.....*1*1
For example n=5
5! = 5 * 4 * 3 * 2 * 1* 0! 
5! = 5 * 4 * 3 * 2* 1 * 1
5! = 120


#include<stdio.h>
int main()
{

    int f, i, n ;

    printf("\nFactorial");
    printf("\nEnter the number(>=0):");
    scanf("%d",&n);

    if(n<0)
    {
        printf("Input not suitable");
        return -1;
    }

    f=1;
    for(i=2;i<=n;i++)
        f*=i;

    printf("%d! is %d",n,f);
    return 0;
}

No comments:

Post a Comment