C++ Program For Recursive Functions
Free C++ course with real-time projects Start Now!!
Program 1
// Program for recursion #include<iostream> #define clrscr() system("cls") using namespace std; int main() { static int i=1,n; if(i==1) clrscr(); if(i>10) exit(0); cout<<i<<endl; i++; main(); }
Program 2
// Program for recursion 1 #include<iostream> #define clrscr() system("cls") using namespace std; void series(int n); int main() { int n; clrscr(); cout<<"Enter a number" ; cin>>n; series(n); } // Recursion void series(int n) { static int i=1; if(i>n) return; cout<<i<<endl; i++; series(n); }
Program 3
// Program for factorial using recursion #include<iostream> #define clrscr() system("cls") using namespace std; int factorial(int n); int main() { int n,x; clrscr(); cout<<"Enter a number" ; cin>>n; x=factorial(n); cout<<"\n Factorial is: "<<x; return 0; } // Recursion int factorial(int n) { static int f=1; if(n==0) return(f); f=f*n; n--; factorial(n); }
Program 4
// Program for reverse number using recursion #include<iostream> #define clrscr() system("cls") using namespace std; int reverse(int n); int main() { int n,x; clrscr(); cout<<"Enter a number"; cin>>n; x=reverse(n); cout<<"\n Reverse is: "<<x; if(x==n) cout<<"\n No is palindrom"; else cout<<"\n No is not palindrom"; } int reverse(int n) { static int s=0; int r; if(n==0) return s; r=n%10; s=s*10+r; n=n/10; reverse(n); }
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google