C Program to Find Factorial of a Positive Number

The factorial of a positive integer n is equal to 1*2*3*...n. You will learn to calculate the factorial of a number using for loop in this example.



The factorial of a positive number n is given by:
factorial of n (n!) = 1*2*3*4....n
The factorial of a negative number doesn't exist. And, the factorial of 0 is 1, 0! = 1

Example: Factorial of a Number

#include<stdio.h>
#include<conio.h>
int main()
{
        int a;
        int fact=1;
        printf("Enter an integer: \n");
        scanf("%d", &a);
        if(a<0)
       {
        printf("See Next Program to find factorial of 0 or negative value Thanks\n");
        }
        else
        {
          if(a>0)
        {  
         for(int i=1; i<=a; i++)  
        {  
           fact=fact*i;  
        }  
           printf("Factorial of the %d = %d", a, fact);
           return fact;
        }
        else
       {  
      if(a==0)  
      {  
           printf("See Next Program to find factorial of 0 or negative value Thanks\n");  
       }  
        }
}
getch();
}

Output
Enter an integer: 10
Factorial of 10 = 3628800
This program takes a positive integer from the user and computes factorial using for loop.
Since the factorial of a number may be very large, the type of factorial variable is declared as unsigned long long.
If the user enters negative number, the program displays error message.

Click on the download button to download file

download button

Post a Comment

Thanks For Your Comment. We will reply you as soon as possible ...

 
COOL PROGRAMMING 4 U © 2013. All Rights Reserved. Shared by Cp4U
Top