C Program to find Power of a Number

How to write a C Program to find the Power of a Number using For Loop and While Loop? You can also use the C pow function to achieve the same. The power of a number is represented by a base and an exponent value. The base (a number) is multiplied by itself the number specified by the exponent. For example, 23 = 2 * 2 * 2 = 8. Here, 2 is the base value, and 3 is the exponent.

C Program to find Power of a Number Using For Loop

This example allows the user to enter the number and exponent value. Next, this program will calculate the power of a given number using For Loop.

#include <stdio.h>
 
int main()
{
  int i, Number, Exponent; 
  long pw = 1;
  
  printf("\n Please Enter any Number : ");
  scanf(" %d", &Number);

  printf("\n Please Enter the Exponent Vlaue: ");
  scanf(" %d", &Exponent);
    
  for(i = 1; i <= Exponent; i++)
  {
  	pw = pw * Number;
  }
  
  printf("\n The Final result of %d Power %d = %ld", Number, Exponent, pw);
  
  return 0;
}
C Program to find Power of a Number using For Loop

Within this Program to find the Power of a Number example, We initialized the integer i value to 1. Also, (i <= Number) condition will help the loop to terminate when the condition fails.

User entered integer values in the above power of a number example: number = 3, and exponent = 4.

First Iteration: for(i = 1; i <= 4; i++)
It means the condition inside the C language For Loop (1 <= 4) is True.
pw = 1 * 3 = 3.
i++ means i will become 2.

Second Iteration: for(i = 2; i <= 4; i++)
It means the condition inside the For (2 <= 4) is True.
pw = 3 * 3 = 9.
i++ means i will become 3.

Third Iteration: for(i = 3; 3 <= 4; i++)
pw = 9 * 3 = 27.
i++ means i will become 4.

Fourth Iteration: for(i = 4; i <= 4; i++)
It means the condition inside the For (4 <= 4) is True.
pw = 27 * 3 = 81.
i++ means i will become 5.

Fifth Iteration: for(i = 5; i <= 4; i++).
It means the condition inside the For (5 <= 4) is False. So, the C program compiler will come out of the For Loop and print the output.

The above-mentioned for loop program calculates the power of a positive number in C. However, we need an extra if-else statement to find the power of negative numbers and zero values.

In the example below, if the exponent is 0, the compiler won’t execute the loop; it simply returns the default result, which is 1.

  • If the exponent value is greater than 0 (positive), the for loop iterates and calculates the power by multiplying the base value by itself.
  • If the exponent is negative, it divides the result by the base value.
#include <stdio.h>
int main(void)
{
int i;
double base, exponent, result = 1.0;
printf("Enter Base value = ");
scanf("%lf", &base);

printf("Enter Exponent value = ");
scanf("%lf", &exponent);

if(exponent > 0) {
for(i = 0; i < exponent; i++) {
result *= base;
}
}
else if (exponent < 0) {
for(i = 0; i < -exponent; i++) {
result /= base;
}
}

printf("Result = %.2f", result);
}
Enter Base value = 4
Enter Exponent value = -2
Result = 0.06

C Program to find the Power of a Number Using While Loop

This program to calculate the power of a number allows the user to enter an integer value and the exponent value. Next, this program will calculate the power using While Loop.

#include <stdio.h>
 
int main()
{
  int i = 1, Num, Expo; 
  long Po = 1;
  
  printf("\n Please Enter any Number : ");
  scanf(" %d", &Num);

  printf("\n Please Enter the Exponent Vlaue: ");
  scanf(" %d", &Expo);
    
  while(i <= Expo)
  {
  	Po = Po * Num;
  	i++;
  }
  
  printf("\n The Final result of %d Power %d = %ld", Num, Expo, Po);
  
  return 0;
}

We replaced the For in the above find the power of a number program with the While loop. If you don’t understand C Programming While Loop, please refer to the article here: C While Statement.

Program to find Power of a Number using While Loop

Calculate the power using the pow function

In the following example, we use the built-in math pow() function to calculate the power of a number.

#include <stdio.h>
#include <math.h>
int main(void)
{
double base, exponent;
printf("Enter Base value = ");
scanf("%lf", &base);

printf("Enter Exponent value = ");
scanf("%lf", &exponent);

double result = pow(base, exponent);
printf("Power = %.2f", result);
}
Enter Base value = 9
Enter Exponent value = 2.5
Power = 243.00

C Program to calculate the power of a number using recursion

In our previous examples, we assumed the user enters positive base and exponent values. However, if the user enters negative values, the result is inaccurate.

In the following example, we declared a recursive function to calculate the power of a positive and negative number. Refer to the recursion in c.

#include <stdio.h>
float calculatePower(int n, int e) {
if(e == 0) {
return 1;
}
if(e < 0) {
return 1.0f / calculatePower(n, -e);
}
return n * calculatePower(n, e - 1);
}
int main(void)
{
double base, exponent;
printf("Enter Base value = ");
scanf("%lf", &base);

printf("Enter Exponent value = ");
scanf("%lf", &exponent);

float result = calculatePower(base, exponent);
printf("Result = %.2f", result);
}
Enter Base value = 3
Enter Exponent value = 2
Result = 9.00

Enter Base value = 3
Enter Exponent value = 0
Result = 1.00

Enter Base value = -4
Enter Exponent value = 5
Result = -1024.00

Enter Base value = 5
Enter Exponent value = -2
Result = 0.04

Also Read