C Program to Find Factorial of n

//WAP TO FIND THE FACTORIAL OF THE NUMBER

#include <stdio.h>

long int Rec_Factorial(int);
long int Itr_Factorial(int);

int main()
{
	int n;

	printf("Enter the number :--> ");
	scanf("%d", &n);

	printf("Factorial of %d :-->  %ld (Iteration) \n", n, Itr_Factorial(n));
	printf("Factorial of %d :-->  %ld (Recursion) ", n, Rec_Factorial(n));

	return 0;
}
long int Itr_Factorial(int n)
{
	int i;
	long int product = 1;			//start with 1 not to 0 because if we multiply then product will not becomes zero
	for ( i = 1;i <= n; i++)    //using iterative method
	{
		product = product * i;
	}
	return product;
}

long int Rec_Factorial(int n)
{
	long int product = 1;
	if(n == 1 || n == 0)     //base condition to stop
    {
		return 1;
	}
	else
	{
	 	 return n * Rec_Factorial(n - 1);  //recursive call
	}
	return -1;
}

<< Previous | Next >>