X to the power of Y

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.
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
#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
  // this is where i get lost. Ans =

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



  system("pause");
  return 0;
}

Thank you in advance and any help will be greatly appreciated.
Use a loop and some multiplication.
firedraco answered your question but I just wanted to add something; don't forget to sync cin. http://www.cplusplus.com/reference/iostream/istream/sync/

Also why is your output all the way down on line 25 outside of the Do While(...) loop? That isn't what the assignment asked you to do. OOOOHHHHHAHAHAA I see :D my friend I REALLY like your sense of humor! and if you can afford the potential hit to your grade then I say do it!
Last edited on
a0 = 1   a<>0
a1 = a
an = a * an-1

an = an/2 * an/2


To multiply two numbers c = a * b
Last edited on
closed account (4Gb4jE8b)
remember, we don't do homework problems here.

as for your hint, remember what a power actually means. I'll use the example a^n, which means a multiplied by itself n times.

Grey Wolf wrote:
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


http://www.cplusplus.com/articles/how_to_ask/

at least you were honest and didn't imply that it wasn't a homework question though.
Topic archived. No new replies allowed.