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>
usingnamespace 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.
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!
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.