# x to the power of y

Page 1 of 1

## 6 Replies - 9942 Views - Last Post: 05 January 2011 - 03:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=208150&amp;s=e6241717e10bed84f85ed072d4b98a13&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shreddingdiego

Reputation: 0
• Posts: 9
• Joined: 22-September 10

# x to the power of y

Posted 03 January 2011 - 04:01 PM

Hello all of you, hope you all had a great holiday but I have a question/problem, it has really to do more with a math question than a coding question, at least i think my code is right. The problem goes as is
"Write a program which receives 2 positive integer inputs, x and y, and computes x to the y power. You are not allowed to call any built-in functions from the C Math library; you must write code (involving a loop) which performs this computation yourself. Write the program so that after it computes x to the y power, it asks the user whether or not to quit. The user should input either "yes" or "no". If the user types "yes" then the program terminates, but if the user types "no", then the program repeats itself, asking for new values of x and y, and computing x to the y power for these new values. This process should repeat indefinitely until the user types "yes" when the program asks if it should quit."

How exactly do i get to the power without using the cmath library? My code is like this.
```#include <iostream>
using namespace std;

int main()
{
int x, y;
int Ans;
char userAns;

counter = 0;
do {
cout << "Enter two positive integers: ";
cin >> x >> y;

counter++;

// Prompt to the user if he/she wants to continue.
cout << "  Do you want to continue (y/n)? ";
cin >> userAns;
} while (userAns == 'y' || userAns == 'Y');

//compute the power
// At this point im just playing around with loops see what will work
for (int i = 0; i < x; i++) {
Ans = x * y;
}

cout << x << " to the power of " << y << Ans << endl;

system("pause");
return 0;
}
```

i know that i am suppose to use a loop and some multiplication and I am aware that my loop is probably wrong for the power of X, which is the one thing i am really having trouble with how does the loop exactly work in this problems?

Thank you in advance and any help will be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: x to the power of y

### #2 ishkabible

• spelling expret

Reputation: 1669
• Posts: 5,818
• Joined: 03-August 09

## Re: x to the power of y

Posted 03 January 2011 - 04:13 PM

well let's think about it. x^y of means multiply x by it's self y times right? so if you make a loop that loops y times and multiply some value, say z, that starts at 1 by x that many times then z == x^y right?

edit: as to not confuse any one here im using the '^' operator to mean "to power of" not xor

This post has been edited by ishkabible: 03 January 2011 - 04:19 PM

### #3 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: x to the power of y

Posted 03 January 2011 - 06:04 PM

think of what raising a number to a power is actually doing.

Example :

This can be written out to be :

Hope your following me so far

Now we have a number being multiplied by itself a certain amount of times, sound sort of like a loop right?

```for(/*loop for the number of times you will be multiplying - The Power */)
yourNumber *= yourNumber; //this is just short hand for : yourNumber = yourNumber*yourNumber

```

does that make sense?

EDIT: if you enter the power = 0 you must have a check since any number to the zero power = 1

This post has been edited by ImaSexy: 03 January 2011 - 06:31 PM

### #4 ishkabible

• spelling expret

Reputation: 1669
• Posts: 5,818
• Joined: 03-August 09

## Re: x to the power of y

Posted 03 January 2011 - 06:25 PM

well i like using

```int total = 1;
for(/*loop for the number of times you will be multiplying - The Power */)
total *= yourNumber

```

becuase if you enter a 0 for y it will return 1 when you return total. your way you have to subtract the number of iterations by 1 witch would give you a lower complexity but you have to remember to check for powers of 0 also. not sure witch way is faster, i just think my way is simpler.

This post has been edited by ishkabible: 03 January 2011 - 06:28 PM

### #5 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: x to the power of y

Posted 03 January 2011 - 06:30 PM

Yupp your right, didnt think of having it to the power of zero. Good catch

### #6 shreddingdiego

Reputation: 0
• Posts: 9
• Joined: 22-September 10

## Re: x to the power of y

Posted 04 January 2011 - 07:07 AM

Ok yeah that makes sense now thank you so very much you have no idea how much i appreciate this

### #7 tenchu77491

Reputation: 1
• Posts: 11
• Joined: 24-July 10

## Re: x to the power of y

Posted 05 January 2011 - 03:17 AM

A pretty cool power function to make (as long as you are doing power of twos) is to use bit shifts. You could toss in one of those for fun and the learning experience. Plus they are faster than doing an actual power function!