Beginner problem

hey guys im studying about recursion by myself and i want to make a recursive function that prompts the user to input the base and exponent and generate the final answer . i have no ideas how to do it can anyone help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int recursive(int x, int y);
int main()
{

	/*i did the program iterate form first to find ways how to solve it recursively but still its useless i dont know where to start */    
      /*int total=1;
	int y, x;
	cout<<"Enter base : ";
	cin>>x;
	cout<<"Enter exponent : ";
	cin>>y;
	for(int i=0; i<y; i++)
	{
		total*=x;
	}
	cout<<x<<"^"<<y<<" = "<<total;*/ 
}
int recursive(int x, int y)
{
	
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
 
int rec(int x, int y)
{
	if(y>0)
	{	y--;
		return x*rec(x, y);
	}
	else return 1;
}

int main() 
{ 
	cout << rec(2,5); // 2^5 = 32
	
	return(0);
}
may i ask a question what happening to a recursive call that has 2 parameters // return x*rec(x, y);
Last edited on
First a design consideration. You are dealing with exponents, exponents get large and generally deal with positive numbers. You should therefore use a variable type that stores large positive numbers. To keep it simple, since you are a self professed beginner, we'll ignore negative exponents and rule out floats. I recommend an 'unsigned int' for your data types.
Last edited on
tnk you so much!
Registered users can post here. Sign in or register to post.