Find power of a number in cpp

Find power of a number in cpp  is cpp program to find exponent of a number.

In this tutorial we are going to one simple and easy program find power of a number in cpp.

First we will how to calculate power of a number, program to calculate power of a number in c++.

How to calculate power of a number

To calculate the exponent (power of number) we first take two input values which are the base number and power value. and calculate power by multiplying base value power value times. and print the power of number.

Formula to calculate power of a number

num=num*num…num N

Example :

2= 2 * 2 *2 =8

There are two ways to find power of a number in cpp

Using while loop

Using pow() function

Power of a Number Using while Loop

This is the first method using which we can find power of a number in cpp

While loop in cpp

Algorithm for power of a Number Using while Loop

1. Start
2. Declare variable
3. Take input from user power value ad base value
4. Find power of a number using loop
5. Print power
6. End

C++ Program to Calculate Power of a Number

Output :

Explanation :

1. Execution starts with the main function.

2. Declare the variables

exponent => to store power value

base => to store base value

result => to store final result

3. Take input power and base from user

cin >> base >> exponent;

=>base = 2 , exponent = 2

4. Now calculate power by formula for this we use while loop

At iteration 1

while (exponent != 0) => 2!=0 condition true loop executed

result *= base   => result = 2

–exponent  => exponent  = 1

At iteration 2

while (exponent != 0) => 1!=0 condition true loop executed

result *= base   => result = 4

–exponent  => exponent  = 0

At iteration 3

while (exponent != 0) => 0!=0 condition false

5. Print the result

cout << result;   => 4

Power Using pow() Function

This is the second method using which we can find power of a number in cpp

using pow() function in cpp

Algorithm for power Using pow() Function

1. Start
2. Declare variable
3. Take input from user power value ad base value
4. Find power of a number using pow()
5. Print power
6. End

C++ program Compute power using pow() Function

Output :

Explanation :

1. Execution starts with the main function.

2. Declare the variables

exponent => to store power value

base => to store base value

result => to store final result

3. Take input power and base from user

cin >> base >> exponent;

=>base = 2 , exponent = 2

4. Now calculate power by formula for this we use pow() which finds the power of a number.

result = pow(base, exponent);

5. Print the result

cout << result;   => 4