"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.

New Topic/Question
Reply



MultiQuote








|