/*
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;
}
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
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*/
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<
cout<
c= float(a)/b;
cout<
}
int main(void)
{
int i=10,j=2;
cout<
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*/

Subscribe to:
Posts (Atom)