C programs to calculate the power of a number have been shown here. The power of a number is denoted by $x^n$ where $x$ is the number and $n$ is the power. It signifies that $x$ is needed to be multiplied by itself $n$ times. In C, the loops or inbuilt function like pow() can be used to calculate the power of a number. The recursive approach has also been shown. The algorithm, pseudocode and time complexity of the program have been shown below.

#### Page content(s):

1. Algorithm

2. Pseudocode

3. Time Complexity

4. Program & Output

## 1. Algorithm to calculate the power of a number

1. Take a number x and the power n as input.

2. Multiply x with itself total n times and store the result inside a variable.

3. Display the result.

## 2. Pseudocode to calculate the power of a number

Input : A number $x$ and the power $n$

Output : $x^n$

1. Procedure power($x$, $n$):

2. $p \leftarrow 1$

3. Repeat for $i = 1$ to $n$

4. $p \leftarrow p * x$

5. Return $p$

6. End Procedure

## 3. Time complexity to calculate the power of a number

Time Complexity: O(n)

Where n is the power.

## 4. C Program & output to calculate the power of a number

Code has been copied
/******************************
alphabetacoder.com
C program to calculate power
of a number using iteration
*******************************/
#include <stdio.h>

int main() {
// declare variables
int num, power, result = 1, i;

// take input of the number & power
printf("Enter the number = ");
scanf("%d", & num);
printf("Enter the power = ");
scanf("%d", & power);

// calculate the value
for (i = 1; i <= power; i++)
result = result * num;

// display result
printf("Result of %d^%d = %d", num, power, result);

return 0;
}


Output

#### Case 1:

Enter the number = 4

Enter the power = 3

Result of 4^3 = 64

#### Case 2:

Enter the number = 5

Enter the power = 2

Result of 5^2 = 25

/******************************
alphabetacoder.com
C program to calculate power
of a number using recursion
*******************************/
#include <stdio.h>

// recursive function to calculate
// power of a number
// here the number and power are integers
int calculate_power(n, power) {
if (power < 1) // exit condition
return 1;
else
return n * calculate_power(n, power - 1);
}
int main() {
// declare variables
int num, power, result, i;

// take input of the number & power
printf("Enter the number = ");
scanf("%d", & num);
printf("Enter the power = ");
scanf("%d", & power);

// calculate the value
result = calculate_power(num, power);

// display result
printf("Result of %d^%d = %d", num, power, result);

return 0;
}


Output

#### Case 1:

Enter the number = 4

Enter the power = 3

Result of 4^3 = 64

#### Case 2:

Enter the number = 5

Enter the power = 0

Result of 5^0 = 1

/****************************************************
alphabetacoder.com
C program to calculate power of a number using pow()
****************************************************/

#include <stdio.h>
#include <math.h>

int main() {
// declare variables
float num, power, result = 1;

// take input of the number & power
printf("Enter the number = ");
scanf("%f", & num);
printf("Enter the power = ");
scanf("%f", & power);

// calculate the value
result = pow(num, power);

// display result
printf("Result of %f^%f = %f", num, power, result);

return 0;
}


Output

#### Case 1:

Enter the number = 3.2

Enter the power = 2.5

Result of 3.200000^2.500000 = 18.317869

#### Case 2:

Enter the number = 5

Enter the power = 2.5

Result of 5.000000^2.500000 = 55.901699