Friday 21 June 2013

C Program To Find Factorial

C Program To Find Factorial

/* C program to display factorial of an integer if user enters non-negative integer. */
#include 
int main()
{
    int n, count;
    unsigned long long int factorial=1;         
    printf("Enter an integer: ");
    scanf("%d",&n);
    if ( n< 0)
        printf("Error!!! Factorial of negative number doesn't exist.");
    else
    {
       for(count=1;count<=n;++count)    /* for loop terminates if count>n */
       {
          factorial*=count;              /* factorial=factorial*count */
       }
    printf("Factorial = %lu",factorial);
    }
    return 0;
}
/*
Output 1
Enter an integer: -5
Error!!! Factorial of negative number doesn't exist.

Output 2 
Enter an integer: 10 
Factorial = 3628800


EmoticonEmoticon