View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Find Factorial of a Number in c

This program takes an integer from a user. If user enters negative integer, this program will display error message and if user enters non-negative integer, this program will display the factorial of that number.
#include <stdio.h>
#include <conio.h>
 main()
{
    int n, fact=1;
    unsigned long long int factorial=1;         
    printf("Enter an integer: ");
    scanf("%d",&n);
       for(int i=1;i<=n;i++)    
       {
          fact*=fact*i;          
       }
    printf("Factorial = %d",fact);
    }
    getch();
}
Result
Enter an integer: 5
Factorial = 120