DSA 5.52 Recursion using static variable

PHOTO EMBED

Thu Mar 09 2023 05:08:24 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

#include <iostream>
using namespace std;

//int x= 0;      or .....global variable
int fun(int n)
{
    static int x=0;       //local static variable
    if(n>0)
    {
        x++;
       return fun(n-1) +x;
    }
    return 0;
}
int main() 
{
    int r;
    r = fun(5);
    cout<<r;
    return 0;
}
content_copyCOPY

output : 25