C Program to find Power of a Number

How to write a C Program to find Power of a Number using For Loop, and While Loop. You can also use the C pow function to achieve the same.

C Program to find Power of a Number Using For Loop

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

/* C Program to find Power of a Number using For Loop */
 
#include <stdio.h>
 
int main()
{
  int i, Number, Exponent; 
  long Power = 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++)
  {
  	Power = Power * Number;
  }
  
  printf("\n The Final result of %d Power %d = %ld", Number, Exponent, Power);
  
  return 0;
}
C Program to find Power of a Number using For Loop

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

for(i = 1; i <= Exponent; i++) 
 { 
    Power = Power * Number; 
 }

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

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

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

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

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

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

C Program to find Power of a Number Using While Loop

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

/* C Program to find Power of a Number using While Loop */
 
#include <stdio.h>
 
int main()
{
  int i = 1, Number, Exponent; 
  long Power = 1;
  
  printf("\n Please Enter any Number : ");
  scanf(" %d", &Number);

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

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


 Please Enter any Number : 5

 Please Enter the Exponent Vlaue: 4

 The Final result of 5 Power 4 = 625