Object Oriented Concepts and Programming

Blog is for Object oriented concepts and prgramming. Specially for MCA GTU (Gujarat Technological University) student

RSS
  • Home
  • Edit

Program code for calculating Factorial of a no, How Recursive function will call and showing process of recursion.

Wednesday, April 21, 2010 by Vishal Joshipura | 0 comments
/*
  The Program for calculating factorial of a no.
  Here, Fact is a recursive function
  It will calculate factorial as below

 if user input is 4
 fact(4)        // call from main m=4,n=4;
 fact(4-1)*4 will call function it self as recursion
      fact(3)   m=3   
      fact(3-1)*3
          fact(2)
          fact(2-1)*2
               fact(1)    // it will return 1 to last cll of fact
          (1 * 2)      // 1 comes from upper call's return value
      ((1*2) *3) = (2*3)  //  2 comes from very upper call's return value
( (1*2*3) * 4) 
(6*4)=24                        // 6 comes from upper last call of fact's return values
and at last 24 will return to main


The program code for showing the recursion mechanism of function.
It will maintain stack call internally for manipulating function calls and
values at diffrent call.
*/
#include "iostream"
#include "conio.h"
using namespace std;

int fact(int m)
{
    if(m==1)
        return 1;
    return fact(m-1)*m;
}

int main(void)
{
    int no=5;
    cout<<"Enter No to calculate factorial of it : ";
    cin>>no;
    cout<<"factorial of "<< no <<" is  "<< fact(no);
    getch();
    return 0;
}

Fact, Factorial, Recursive Function

No comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Blog Archive

  • ▼  2010 (10)
    • ►  November (1)
    • ►  August (3)
    • ►  May (2)
    • ▼  April (3)
      • Program code for calculating Factorial of a no, Ho...
      • Startup code for C/C++, How actual program execuio...
      • How To Implement Default Argument in Function.
    • ►  March (1)

Followers

Labels

  • Argument (1)
  • Class (1)
  • Defualt Argument (1)
  • Fact (1)
  • Factorial (1)
  • Item (1)
  • OOCP Material (1)
  • Pass by address (1)
  • Pass By argument (1)
  • Recursive Function (1)

Contributors

  • Dr. Parag Shukla
  • Vishal Joshipura
  • vaibhav

Pages

  • Home
Copyright © 2010 Object Oriented Concepts and Programming