C Program to Find Factorial of a Number

#include <stdio.h>
int main()
{
    int n, i;
    unsigned int factorial = 1;

    printf("Enter an integer: ");
    scanf("%d",&n);

  
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");

    else
    {
        for(i=1; i<=n; i++)
        {
            factorial *= i;              
        }
        printf("Factorial of %d = %u", n, factorial);
    }

    return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800

No comments:

Post a Comment