Write c++ program that find the x raised to the power n use the function concept ,
where n can be any integer. Use the algorithm that would compute x 20 by multiplying
1 by x 20 times
#include <iostream>
double my_pow(double x , int n )
{
if (n == 0)
return 1;
double res = x;
for (int i = 1; i < n; i++)
{
res *= x;
}
return res;
}
int main()
{
double x;
int n;
std::cout << "Enter x : ";
std::cin >> x;
std::cout << "Enter n : ";
std::cin >> n;
double new_x=my_pow(x, n);
std::cout << "Result : " << new_x << std::endl;
return 0;
}
Comments
Leave a comment