Recursion in DSA C++

Program 1

// Recursion Implementation 
#include<iostream>
#define MAXSIZE 5
using namespace std;
int factorial(int n);
int main()
{
    system("cls");
    int n,f;
    cout<<"\n Enter the number";
    cin>>n;
    f=factorial(n);
   cout<<"\n Factorial is : "<<f;
    return 0;
}

int factorial(int n)
{
     static int f=1;
     if(n==0)
     return(f);
     f=f*n;
     n--;
     factorial(n);
}




















// Series from 1 to n using recursion 

// void display(int n);
// int main()
// {
//     int n;
//     system("cls");
//     cout<<"Enter  the limit: ";
//     cin>>n;
//     display(n);
//     return 0;
// }
// void display(int n) // n=10
// {
//     static int i=1;  // i=2 
//     if(i>n)  
//      return; 
//     cout<<"\n"<<i;      // 1  2 
//     i++;   // 3  
//     display(n);
// }

 

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *