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

Startup code for C/C++, How actual program execuion starts.

Saturday, April 10, 2010 by Vishal Joshipura | 1 comments
Startup code for C/C++ programs usually consists of the following actions, 
performed in the order described:

1.Disable all interrupts.

2.Copy any initialized data from ROM to RAM.

3.Zero the uninitialized data area.

4.Allocate space for and initialize the stack.

5.Initialize the processor's stack pointer.

6.Create and initialize the heap.

7.Execute the constructors and initializers for all global variables (C++ only).

8.Enable interrupts.

9.Call main.

After Calling main it doesnt stop but also include few instruction after calling main and
then your code will start working....


How To Implement Default Argument in Function.

Thursday, April 8, 2010 by Vishal Joshipura | 1 comments
/*
This code shows how to use default argument in function
or how to create an optional parameter to the function

*/

#include

void div(int a,int b=1)
{
    float c;
    cout<<<"A is "<
    cout<<<"B is "<
    c= float(a)/b;
    cout<<<"A / B is "<
}


int main(void)
{
    int i=10,j=2;
    cout<<<"Default Argument";
    div(i,j);
    div(i);
    return 0;
}

/* Here Second parameter of div() function is
optional we can ommit it autometically consider
as Default argument value as 1*/

Defualt Argument

Subscribe to: Posts (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