Newbie question

not exactly sure how I would raise 10 to the power of the user's number without using the pow function. am i missing something?

write a recursive function that raises 10 to the power of some number. Example 10 to the power of 2 is 100. Do not use the pow function
That would be simple.

You pass the power and result of base * 10 to a recursed function, decrementing it the power with each recursion and then return the result when it hits 0 (or would it be -1? not sure which, but that is how I would do it).

The function prototype would be:

int raiseten( int base, int power );

Or something similarly named.
Last edited on
You know how to raise something to the power of something else, don't you? (you should know if you passed elementary school). Well, then you know how to write a program that does that.
cant really picture what your saying

base starts at 10, since that is the base you're using.

You have a power that is input by the user.

You can't just multiply 10 * 10 because the answer would always be 100. You need to carry the result of the calculation into the next run the function.

You can't recurse indefinitely, so you pass the number representing the power as a counter and subtract 1 for each time you raise the base by 10.
ahhhh thank you
This is what I have so far, is there anyway to get rid of the loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int power,result,number;
    cin>>power;
    
    result=recur(power);
    cout<<result;
    system("pause");
}

int recur(int power){
    
     int number=10;
     
     if(power==0){
         return 0;
     }else
         while(power>1){
             number=number*10;
             power-=1;
             recur(power);
         }
         return (number);
}

Yes, you can pass the result of the calc and the power, instead of passing the power alone and running a loop to calculate the result.

I gave you the prototype for the function.

Keep in mind also, any number raised to the power of 0 is 1.
Last edited on
Code was wrong ...OOPS.
Last edited on
@Subzero030201
That's almost the answer. The only problem is that you're not actually multiplying 10 by the previous answer, but always just by ten... You're always going to get 100 back.
Either make number static or multiply it by the return value of recur:

1
2
3
4
5
6
7
8
9
10
11
int recur(int Power)
{
if(Power == 0)
{
return 1;
}
else 
{
return 10*recur(Power-1);
}
}
It might work now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <Windows.h>
using namespace std;

int recur( int power, const int number)
{
    
 static int prod=1;

         if(power==0)
         {
          return (prod);
         }
	 else if(power>=1)
	  {
             prod*=number;
             recur(power-1,number);
          }
        
}


int main()
{
    int power,result,number;
	cout<<"Enter your base:  ";
	cin>>number;
 	cout<<"Enter your power:  ";
	cin>>power;
       
       result=recur(power,number);
       cout<<"Your result is : "<<result<<endl;
       system("pause");
       return 0;
}



Last edited on
Or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


#include <iostream>
#include <Windows.h>
using namespace std;
int recur( int power, const int number);

int main()
{
    int power,result,number;
	cout<<"Enter your base:  ";
	cin>>number;
 	cout<<"Enter your power:  ";
	cin>>power;
       
       result=recur(power,number);
       cout<<"Your result is : "<<result<<endl;
       system("pause");
       return 0;
}

int recur( int power, const int number)
{
    
        static int prod=1;

         if(power==0)
         {
           return (prod);
         }
	 else if(power>=1)
	  {
             prod*=number;
             recur(power-1,number);
          }
         
}








Last edited on
I meant something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int recur(int p)
{
	static int number = 1;
	if(p == 0)
	{
		return number;
	}
	else
	{
		number *= 10;
		recur(p-1);
	}
}
Ok code should be good now.
Is the base always 10 or do you need to make it flexible enough for any base?

You pretty much got it, here is my solution(works only with base 10 though):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int raiseten( int, int );

int main( )
{
	int base = 1;
	int exponent;
	int result = 0;

	cout << "Enter an exponent to raise 10 by: ";
	cin >> exponent;

	result = raiseten( base, exponent );

	cout << result;
	cin.ignore();
	cin.get();

}

int raiseten( int base, int power )
{
	if (power == 0)
		return base;
	else
		return raiseten( base * 10, --power );
}


I'm sure this could be adapted. It certainly should be changed so that you're not passing 1 to the function but the actual and power and a 1 should be returned if it is a power of zero. I think three parameters would be needed in such a case: result, base, power.



Last edited on
Topic archived. No new replies allowed.