Puppy and Sum
Problem Code: PPSUM
Actually…i guess this can be done iteratively easily… but i tried recursive approach
Can you please tell me…where my code fails ???
Problem Code: PPSUM
Actually…i guess this can be done iteratively easily… but i tried recursive approach
Can you please tell me…where my code fails ???
#include<bits/stdc++.h>
using namespace std;
int one_to_n(int n)
{
if(n == 1)
{
return 1;
}
return n + one_to_n(n-1);
}
long long tu_bta(int d,int n)
{
if(d == 1)
{
// toh mtlb (n) tak ka sum from 1 to n
return one_to_n(n);
}
int iss = one_to_n(n);
return tu_bta(d-1,iss);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int d,n;
cin>>d>>n;
if(n == 1)
{
cout<<(d*n)<<endl;
continue;
}
long long res = tu_bta(d,n);
cout<<res<<endl;
}
return 0;
}