Posts

Showing posts from February, 2021

C++ program for tower of hanoi using recursion

C++ program for tower of hanoi using recursion #include <iostream> using namespace std; void toh(int n,char source,char destination,char helper){     if(n==0){         return;     }     toh(n-1,source,helper,destination);     cout<<"move from"<<" "<<source<<"to"<<" "<<destination<<endl;     toh(n-1,helper,destination,source); } int main() {     int n;     cin>>n;     toh(n,'A','B','C');     return 0; }  

find first and last occurence of a number in an array using recursion in c++

first and last occurence of a number in an array using recursion in c++ #include <iostream> using namespace std; int f_occur(int a[],int n,int s,int i){   //s is the required number to be find.             if(a[0]==s){                  return i;          }     else{                  return f_occur(a+1,n-1,s,i++);              } } int l_occur(int a[],int n,int s,int i){   //s is the required number to be find.             if(a[n-1]==s){                  return i;          }     else{          i--;        return l_occur(a+1,n-1,s,i--);              } } int main() {     int a[50],n...

C++ program to check whether the array is sorted or not using recursion (without recursion)

Check if the given array is sorted or not using recursion in C++ #include <iostream> using namespace std; bool sort(int a[],int n){     if(n==1){         return 1;     }     else{         return (a[0]<a[1]&&sort(a+1,n-1));     } } int main() {     int a[50],n,i;     cin>>n;     for(i=0;i<n;i++){         cin>>a[i];     }     cout<<sort(a,n);     return 0; }  Check if the array is sorted or not without using recursion #include <iostream> using namespace std; void sort(int a[],int n){     int i;     for(i=0;i<n;i++){         if(a[i]<a[i+1]){             continue;         }         else{             break;         }  ...

write a c++ program to find nth fibonacci number using recursion and write a c++ program to print fibonacci series using recursion

C++ program to find nth fibonacci number using recursion  #include <iostream> using namespace std; int fib(int n) {        if(n==0 || n==1){         return n;     }     else     {            return (fib(n-1)+fib(n-2));     } } int main() {     int n;     cin>>n;     fib(n);     cout<<fib(n);     return 0; } C++ program to print fibonacci series using recursion #include <iostream> using namespace std; int fib(int n) {        if(n==0 || n==1){         return n;     }     else     {          return (fib(n-1)+fib(n-2));     } } int main() {     int n,i;     cin>>n;     for(i=0;i<n;i++){     fib(i);     cout<<fib(i)<<" "; }   ...