C++ Recursion
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
/*
Recursion in cpp
direct
int main()
{
display();
}
void display()
{
----
---
display();
}
indirect
display()
{
xyz();
}
xyz()
{
display();
}
*/
#include<iostream>
using namespace std;
int main()
{
static int i=1;
if(i>n)
{
exit(0);
}
cout<<i<<endl;
i++;
main();
return 0;
}Program 2
/*
Recursion in cpp
direct
int main()
{
display();
}
void display()
{
----
---
display();
}
indirect
display()
{
xyz();
}
xyz()
{
display();
}
*/
#include<iostream>
using namespace std;
void display(int n);
int main()
{
int n;
system("cls");
cout<<"Enter the limit";
cin>>n; // n=5
display(n); // n=5
return 0;
}
void display(int n) // n=5
{
static int i=1; // i=3
if(i>n)
return;
cout<<i<<endl; //1 2 3
i++; //3
display(n);
}Program 3
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
system("cls");
int n,x;
cout<<"Enter a number";
cin>>n;
x=factorial(n); // n=5
cout<<"\n Factorial is of: "<<n<< " is: "<<x;
return 0;
}
int factorial(int n) // n=0
{
static int f=1; // f=120
if(n==0)
{
return(f);
}
f=f*n;
n--; //n=0
factorial(n) ;
}
// Reverse of number using recursion
// palimdrome number using recursion
// armstrong number using recursion We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

