Thursday, 18 September 2025

RECURSION PROBLEMS

#include<iostream>

using namespace std;

void pr(int n,int s=1){

    if(s>n) return;

    cout<<s<<endl;

    pr(n,++s);

}

int power(int n,int a){

    if(a==1) return n;

    return n*power(n,a-1);

    

}

int fibo(int n){

    if(n==1||n==2)return 1;

    return fibo(n-1)+fibo(n-2);

}

int powlogarithmic(int n,int a){

    int r=a%2;

    if(a==1) return n;

    if(a==2) return n*n;

    int l=powlogarithmic(n,a/2);

   if(r==0) return l*l;

   return l*l*n;

}

/// "Given a staircase with n steps, and you can climb either 1 or 2 steps at a time, how many distinct ways are there to reach the top?"////////

int stair(int n,int s=0){

   

    if(s>=n-1) return 1;

    return stair(n,s+1)+stair(n,s+2);

}


int main(){

//   cout<<power(3,4)<<endl;

//   cout<<fibo(6)<<endl;

//   cout<<powlogarithmic(3,17);

   cout<<stair(4);

No comments:

Post a Comment

SDE floyd-warshall algorithm

 // User function template for C++ class Solution {   public:     void floydWarshall(vector<vector<int>> &dist) {         //...