Two int numbers are entered through the keyboard. Write a c++ program to find the value of one number raised to the power of another. (Do not use the pow() function.
Ex: Enter two int numbers: 5 3
5 * 5 * 5 = 125
Ex: Enter two int numbers: 5 -3
1/(5 * 5 * 5) = 1/125 = 0.008
Ex: Enter two int numbers: -2 -3
1/(-2 * -2 * -2 )= 1/-8 = -1/8 = - 0.125
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
main.cpp
#include <iostream>
using namespace std;
double myPowerFunction(double x,int power){
double res = 1;
for(int i=1;i<=power;i++){
res*=x;
}
return res;
}
int main(void)
{
cout<<"Enter base : ";
int base;
cin>>base;
cout<<"Enter power : ";
int power;
cin>>power;
double res = myPowerFunction(base,power);
cout<<base<<"^"<<power<<" =
"<<res;
return (0);
}
Get Answers For Free
Most questions answered within 1 hours.