In C What is wrong with the following program include using
In C++, What is wrong with the following program?
#include <iostream>
using namespace std;
int f(int n){
int result = 5+5*f(n-1);
return (result);
}
void main(){
int r, n;
cout << “Enter value for n:” << endl;
cin >> n;
r = f(n);
cout << “Result is:” << r << endl;
}
Solution
now i would like to explain basic concept
there are 2 types of cases when using a recursive function: base cases and recursive cases. This is very important to remember when using recursion, and when you are trying to solve a problem you should ask yourself: “What is my base case and what is my recursive case
base case for exit and recusive for solve problem.
Here , i modify function as.
int f(int n) {
and complete program:
#include <iostream>
#include <conio.h>
using namespace std;
int f(int n){
if(n==0)
return 5;
else{
int result= 5 + 5*f(n-1);
return result;
}
}
void main()
{
int r,n;
cout << \"Enter value for n\ \";
cin >>n;
r=f(n);
cout <<\"result is = \" << r << endl;
getch();
}

