I need to write a program that will compute a number (base) raised to a power which can be an integer, negative or zero with out including the math library. This is what I wrote thus far. Please help me.
#include <iostream>
using namespace std;
int main()
{
double x, y;
cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;
double i = 1;
double product = x;
while ( y > i)
{
product *= x;
i++;
}
return 0;
}
My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.
#include <iostream>
using namespace std;
int main()
{
double x, y;
cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;
double i = 1;
double product = x;
while ( y > i)
{
product *= x;
i++;
}
return 0;
}
My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.
Comment