C++ program to find the power of a number using loop
Source Code:
#include <iostream>
using namespace std;
int main()
{
int exponent;
float base, result = 1;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = ";
while (exponent != 0) {
result *= base;
--exponent;
}
cout << result;
return 0;
}

Finding power of a number in C++:
Here, we are going to learn how to find the power of a number using loop in C++?
Here, we are going to calculate the value of Nth power of a number without using pow function.
The idea is using loop. We will be multiplying a number (initially with value 1) by the number input by user (of which we have to find the value of Nth power) for N times. For multiplying it by N times, we need to run our loop N times. Since we know the number of times loop will execute, so we are using for loop.
Power of a Number Using Non-Recursive C++ Program
The program to find the power of a number using a non-recursive program is given as follows −
Example
#include<iostream>
using namespace std;
int power(int x, int y) {
int i,power=1;
if(y == 0)
return 1;
for(i=1;i<=y;i++)
power=power*x;
return power;
}
int main() {
int x = 3;
int y = 4;
cout<<"x = "<<x<<endl;;
cout<<"y = "<<y<<endl;
cout<<"x^y = "<<power(x,y);
return 0;
}
x = 3 y = 4 x^y = 81
In the above program, the function power() is used to calculate the power of a number. It is a non-recursive function. In the function, a for loop is used which runs from 1 to y. For each iteration of the loop, x is multiplied with power.
So, x is multiplied with itself y times and the result is stored in power. This leads to x^y being stored in power. Then power is returned to the main() function.
The following code snippet demonstrates this −
int power(int x, int y) {
int i, power = 1;
if(y==0)
return 1;
for(i=1;i<=y;i++)
power = power*x;
return power;
}
In main(), the values of x, y and x^y are displayed. This is shown in the code snippet given below −
cout<<"x = "<<x<<endl;; cout<<"y = "<<y<<endl; cout<<"x^y = "<<power(x,y);
Power of a Number Using Recursive Program
The program to find the power of a number using a recursive program is given as follows.
Example
#include<iostream>
using namespace std;
int power(int x, int y) {
if (y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
int main() {
int x = 3;
int y = 4;
cout<<"x = "<<x<<endl;;
cout<<"y = "<<y<<endl;
cout<<"x^y = "<<power(x,y);
return 0;
}
Output
x = 3 y = 4 x^y = 81
In the above program, power() is a recursive function. If the value of y is 0, it returns 1. If y is even, it recursively calls itself with the values x and y/2 and returns power(x, y/2)*power(x, y/2). If y is odd, it recursively calls itself with the values x and y/2 and returns x*power(x, y/2)*power(x, y/2). This is demonstrated by the following code snippet.
int power(int x, int y) {
if (y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
In main(), the values of x, y and x^y are displayed. This is shown in the code snippet given below.
Post a Comment