Pages

Monday, January 2, 2012

C Programming to find factorial of a number using function


/* to find factorial of a number */
#include<stdio.h>
#include<stdlib.h>
long factorial(int);
int main()
{
    int n;
    printf("Enter the number: ");
    scanf("%d",&n);
    if (n<0)
        printf("Factorial of %d is not possible",n);
    else if (n==0)
        printf("Factorial of 0 is 1");
    else
        printf("Factorial of %d is %d",n,factorial(n));
    system("pause");
    return 0;
}
long factorial(int n)
{
    int i,fact=1;
    for(i=1;i<=n;i++)
    fact*=i;
    return fact;
}

No comments:

Post a Comment