C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Showing posts with label Factorial of number by Recursion Method. Show all posts
Showing posts with label Factorial of number by Recursion Method. Show all posts

Wednesday, December 11, 2013

Program Factorial by Loop and Recursion Versions

 Program FACTORIAL by RECURSION METHOD
Recursive Function Factorial of number in C++ recursion version
Recursive Function Factorial of number in C++ programming recursion version


What is Recursion

When a function in C++ calls itself, it is called Recursive Function and the process of calling a function itself is called Recursion.
A method of defining a function in terms of its own definition is called recursion.

Advantages of using Recursion in C++

  1. Recursion is a good problem solving technique in programming
  2. It is based on the concept to solve a problem by reducing the problem to smaller sub-problems;
  3. This results in recursive calls. In each successive recursive call the problem is reduced to more smaller sub-problem.
  4. Recursive algorithms are simple to understand
  5. Recursive algorithms are easy to implement


• Example: the Fcatorial of a non negatve number

  Base case in Recursion

   Recursion needs a base-case in order to stop

Recursive case

Recursion needs a recursive case to call a function itself to reduce problem into sub-problems
• fact(n) = n x fact(n-1)       Recursive part / recursive case of recursion
• fact(0) =  1                Terminating condition part also called base case
• In programming recursion is a method call to the same method. In other words, a recursive method     is one that calls itself.
• Why write a method that calls itself?

• But! Recursive calls can result in a an infinite loop of calls
• recursion needs a base-case in order to stop    , example fact(0) = 1, if n=0 then factorial =1
as we know that factorial of 0 is 1.
A recursive function to find factorial of a number works by calling itself
Example: if n>0 the factorial(n) = n x factorial (n-1)
as we know n! = n x (n-1)!

 /*
Write a C++ Program to input a positive number,
Calculate and display factorial of this number
by recursion.

For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ComputerGap.com
*/
#include<iostream.h>

#include<conio.h>

long  factorial(int n);
void main()
{

  clrscr();

  int number, counter;
 

  label1:
 
  cout<<"\n Enter the Number = ";

  cin>>number;

  if ( number < 0)
  {
  cout<<"\n Enter a non negative number, please!";
  goto label1;
  }
  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial(number);

  getch();

}

long factorial(int n)

{

    if ( n == 0 )
        return 1;
    else
        return n * factorial(n-1);
}
 


Factorial of a number in C++ for loop version for understanding Recursion Logic


/*
Write a C++ Program to input a positive number
Calculate and Display Factorial of this number
by For Loop.


For Free C++ Programming Techniques (Example Programs), visit
Www.EasyCppProgramming.Blogspot.Com

and For Good Notes
Visit   Www.ForFreeEducation.Blogspot.Com
*/
#include<iostream.h>

#include<conio.h>

void main()
{

  clrscr();

  int number, counter;
  long int factorial;

  cout<<"\n Enter the Number = ";

  cin>>number;

  factorial = 1;
  for (counter = 1; counter <= number; counter++)
           factorial = factorial * counter;

  cout<<"\n\n ----------- Results ------------";

  cout<<"\n\n The Factorial of the number "<<number<<"\n is "<<factorial;

  getch();

}



You can download and install Turbo C++ 3.0 Compiler here
You can read the use of Writing, compiling, executing programs in C++ in Turbo C++ here

Download and Install Turbo C++ Full Screen Windows 7 Vista

You can find calculate factorial of a number program with while loop here
You can find calculate factorial of a number program with do while loop here

You can find calculate factorial of a number program using Recursion ( Recursive Factorial Function )

Share:

EasyCPPprogramming.blogspotcom

Labels