Calculation of factorial

Program Description

The program takes an integer input from the user and calculates the factorial of that number. The factorial() function expects only integer inputs.No error checking is performed and all inputs are assumed to be valid.

Program
/*******************************
*  code.cheraus.com
*  
* Calculation of factorial
* 
********************************/
#include<stdio.h>

//function prototype
int factorial(int fact);

int main()
{
   int fact,ans;
   
   
   printf("\nEnter the number : ");
   scanf("%d",&fact);
   
   
   if(fact>=0)
   {
     ans = factorial(fact);
     printf("\nThe factorial is of %d is %d\n",fact,ans);
   }
   else
   {
     printf("\nEnter a Valid Number\n");
   }
}

/*
Definition for factorial function

Function: fctorial() .The function calculates the factorial of a 
           given integer value.
		   
Inputs  : The function takes one integer as an argument.
Return  : The function returns the factorial of the given number to
           the calling function.

*/

int factorial(int fact)
{
   if(fact>=2)
   {
     return(fact*factorial(fact-1));
   }
   else
     return 1;
}

Output
Test Case 1:


Enter the number : 6
The factorial is of 6 is 720
Test Case 2:

Enter the number : 5
The factorial is of 5 is 120

Comments
comments powered by Disqus