Developer & Internet Marketer

Welcome to my blog.

Sed sed leo sit amet elit sagittis fermentum. Nulla posuere commodo erat. Nam ac nunc ipsum, id espara tincidunt orci. Quisque adipiscing molestie dolor, ut congue sem porta et. Sed sapien urna, auctor et accumsan ut, accumsan sit amet dui. Nunc tristique interdum magna, dictum volutpat sem. Nam ac nunc ipsum, id espara tincidunt orci. Sed sapien urna, auctor et accumsan ut.

C++ Program to Calculate Factorial using Recursion

May 3, 2017wp-himu0 Comments

Factorial Definition: Factorial is the result of multiplication of N number of consecutive integers from 1 to the N. In equations, it is symbolized by an exclamation mark “!”. For example, 5! = 1 * 2 * 3 * 4 * 5 = 120.

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5!=5*4*3*2*1.

Remember, the value of 0! is 1, according to the convention for an empty product.

C++ Program - Factorial using Recursion

C++ Program – Factorial using Recursion

[codesyntax lang=”cpp” title=”C++ Program – Factorial using Recursion” bookmarkname=”C++ Program – Factorial using Recursion”]

#include <iostream.h>
int factorial(int);
void main(void)
	int number;
	cout << "Please Enter a positive integer: ";
	cin >> number;
	if (number < 0)
		cout << "That is Not a positive integer! \n";
		cout << number << " factorial is: " << factorial(number) << endl;

int factorial(int number)
	int temp;
	if(number <= 1) return 1;
	temp = number * factorial(number - 1);
	return temp;


How This Program Works?

The above C++ Factorial program asks the user to enter a positive integer. If the number entered by user is negative (means less than 0, Zero) then the program displays a message “number is not a positive integer” else it calls the factorial().

Now the real calculation is done. The factorial function i.e. factorial() checks if the given number is equal to or less than 1 or not, if yes then it returns 1 to display factorial result as 1 otherwise recursion take place.

What is Recursion? How this C++ Program Calculates Factorial? 

In recursion, a function call itself and the called function again calls itself. Thus the same function is called again and again by itself until some condition terminates this. To understand recursion let’s take the above code example (Please first prepare yourself for this. Caution: Concentration Needed).

Suppose, we want to know the factorial of 3 using our C++ recursion program (that means number variable stores 3). The factorial function, factorial(int number) or factorial(3) will be called.

Now the function body of factorial(3) contains temp= number*factorial(number-1) which means temp = 3*factorial(2) to calculate factorial. Now look at this statement, the factorial function is called again in this statement (remember the recursion?) but with an argument 2 (which means number variable now stores 2).

Now again, temp = number*factorial(number-1) will be executed (remember number variable now stores 2) i.e. temp = 2*Factorial(1).

Now, The function factorial() with an argument 1 is called (this means number variable now stores 1), but it will get terminated with a return value 1 as coded (see the if condition when number stores 1 or less). This value will be return 1 to factorial(2) (Note: return occurs in factorial(1) therefore this function terminates and returns to the function who called it, which is factorial(2)).

Now we have to back track our steps (please pay attention here), Suppose, function factorial(2) is running and is now at the statement temp = 2*Factorial(1), which is equal to temp = 2*1 (remember factorial(1) returned 1). Now the result of multiplication (i.e. 2) will be stored in temp and this temp value will be returned to function factorial(3).

Now the function factorial (3) is executing and is at statement temp = 3*Factorial(2) which is equal temp = 3*2. Now the result of multiplication (i.e. 6) will be stored in temp variable and this temp value will be returned to function main() .

Huh! Now we had successfully calculated the factorial of 3 which is stored in temp (i.e. 6) and now the program control will be returned to calling function at the main(). The C++ statement cout << number << ” factorial is: ” << factorial(number) << endl; executes and factorial is displayed. Finished!

I know this was tricky but please read again. I had tried to make it simple and as detailed as possible. I hope this Factorial calculator Program is understandable. To understand this C++ program more clearly, do a little research on function recursion and read this again and again. I had tried my best to enlighten the working of program and its logic so study again.

Leave a Reply
www.edatastyle.com www.introducingsomething.com