C++ program to find Factorial of a number.

 

Example of Factorial:
*************************************
Factorial of 1: 1
Factorial of 2: 2 x 1 = 2
Factorial of 3: 3 x 2 x 1 = 6
Factorial of 4: 4 x 3 x 2 x 1 = 24
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
Iterative Factorial of n: n x (n-1) x (n-2) x . . . . .1
Recursive Factorial of n: n x Factorial of n-1
*************************************
Iterative program for factorial:

#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();

	int num, i, fact=1;

	cout<<"Enter an integer greater than or equal to 1: ";
	cin>>num;

	for(i=num; i>=1; i--)
	{
		fact = fact*i;
	}

	cout<<"Factorial of "<<num<<" is: "<<fact;

	getch();
}

Recursive program for factorial:

#include<iostream.h>
#include<conio.h>

int factorial(int n);

void main()
{
	clrscr();

	int num, fact;

	cout<<"Enter an integer greater than or equal to 1: ";
	cin>>num;

	fact = factorial(num);

	cout<<"Factorial of "<<num<<" is: "<<fact;

	getch();
}

int factorial(int n)
{
	int answer;

	if(n==1)
	{
		answer = 1;
	}
	else
	{
		answer = n * factorial(n-1);
	}

	return(answer);
}