This is a C Program to find out the power of any number using loop.
We have to create a C Program which calculates the power of any number, by taking the values of base and exponent from the user.
We have to take two inputs from the user, base and exponent. We have to run a loop from 1 to exponent and calculate the power by multiplying power with base in every iteration.
1. When base and exponent both are positive
If the user provides the value of Base as 2 and Exponent as 5, then the power i.e base^exponent will be 32
2. When Exponent = 0
As we know if the Power of any number is 0, its value is = 1. Here if the user enters Base as 5 and Exponent as 0, the output will be 1.
3. When Base is Negative
If the user enters a negative Base say -2 and Exponent as 3, we will have -8 as Power in this case.
4. When Exponent is Negative
If the user enters a negative Exponent say -2 and Base as 3, we will have 0.11111 as Power in this case as the equation will become 1/(3^2).
Here is source code of the C Program to find the power of a number using a loop. The program is successfully compiled and tested using Codeblocks gnu/GCC compiler on Windows 10. The program output is also shown below.
* C program to find power of any number using for loop
int base, exponent;
float power = 1;
/* Take base and exponent as input*/
printf("Enter base: ");
printf("Enter exponent: ");
int expo = exponent;
while (expo < 0)
power = power/base;
/*Calculate power */
for(i = 1; i <= exponent; i++)
power = power * base;
printf("%d ^ %d = %f", base, exponent, power);
1. In order to calculate the power of a number using a loop we have to take two inputs from user, base and the exponent. We have to initialize a variable named power as 1.
2. Power variable stores the final value of the power.
3. Run a loop starting from index value 1 till the value of exponent, and multiply power with base and store the result in power in each iteration. After it comes out of the loop, power has the output value in it, we just need to print that.
1. Enter base: 2 Enter exponent: 5 2 ^ 5 = 32.000000 2. Enter base: 5 Enter exponent: 0 5 ^ 0 = 1.000000 3. Enter base: -2 Enter exponent: 3 -2 ^ 3 = -8.000000 4. Enter base: 3 Enter exponent: -2 3 ^ -2 = 0.111111
Sanfoundry Global Education & Learning Series – 1000 C Programs.