Power of a Number in C is usually represented with a base and an exponent. The base tells us what number is being multiplied and exponent tells us how many times the number is being multiplied.
So, a power is the product of multiplying the number by itself.
Examples:
5^3 = 125. Here, 5 is being multiplied to itself 3 times. i.e. 5*5*5 = 125.
2^4 = 16. Here, 2 is being multiplied to itself 4 times. i.e. 2*2*2*2 = 16.
Write a C Program which calculates the power of any number, by taking the values of base and exponent from the user.
1. Take two numbers base and exponential as input.
2. Run a loop starting from index value 1 till the value of exponent.
3. In each iteration of the loop, multiply the base to itself.
4. Print the final result and exit.
There are several ways to find the power of a number in the C language. Let’s take a detailed look at all the approaches to finding the power of a number in C.
- Find Power of a Number in C using While Loop
- Find Power of a Number in C using For Loop
- Find Power of a Number in C using Recursion
- Find Power of a Number in C using pow() Function
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).
In this approach, we find the power of a number using while loop.
Here is source code of the C Program to find the power of a number using a while 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 while loop
*/
#include <stdio.h>
int main()
{
int base, exponent;
float power = 1;
/* Take base and exponent as input*/
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
int expo = exponent;
//to calculate the power of negative exponents
while (expo < 0)
{
power = power/base;
expo++;
}
//To calculate the power of positive exponents
while(expo>0)
{
power = power * base;
expo--;
}
printf("%d ^ %d = %f", base, exponent, power);
return 0;
}
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. If the exponent is negative, run a while loop till the value of exponent is less than 0. In each iteration calculate power by dividing power by base and increment the value of exponent by 1.
4 If the exponent is positive, run a while loop till the value of exponent is greater than 0. In each iteration calculate power by multiplying power by base and decrement the value of exponent by 1.
5. After coming out of the loop print the value of power.
Example:
1. Consider an example having base = 5 and exponent = 3. Store the value of exponent in variable expo i.e., expo=3.
2. Declare another variable power and assign it to 1.
3. As the value of expo is greater than 0 run the second while loop.
- When the value of base is 3 enter the while loop and calculate power so, power = power * base i.e., power = 1*5 = 5 and decrement the value of expo by 1.
- Now, expo=2 again enter the while loop and repeat the same process. So, power = 5*5 = 25 and expo = expo– = 1.
- Now, expo=1 again enter the while loop and repeat the same process. So, power = 25*5 = 125 and expo = expo– = 0.
- Now, expo = 0, check the condition in while loop, as it is not greater than 0 come out of the loop.
4. Print the value of power.
Time Complexity: O(n)
The above program for finding power of a number has a time complexity of O(n), where n is the value assigned to the exponent.
Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Testcase 1: In this case, we enter the base and the exponent, both values are positive, that is, the value of the base is “2” and the value of the exponent is “5”.
Enter base: 2 Enter exponent: 5 2 ^ 5 = 32.000000
Testcase 2: In this case, we enter the base and the exponent, but here exponent value is 0 that is, the value of the base is “12” and the value of the exponent is “0”.
Enter base: 12 Enter exponent: 0 12 ^ 0 = 1.000000
Testcase 3: In this case, we enter the base and the exponent values, but here base value is negative number, that is, the value of the base is “-5” and the value of the exponent is “3”.
Enter base: -5 Enter exponent: 3 -5 ^ 3 = -125.000000
Testcase 4: In this case, we enter the base and the exponent, but here exponent value is negative number, that is, the value of the base is “4” and the value of the exponent is “-2”.
Enter base: 4 Enter exponent: -2 4 ^ -2 = 0.062500
In this approach, we find the power of a number using for loop.
Here is source code of the C Program to find the power of a number using a for 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
*/
#include <stdio.h>
int main()
{
int base, exponent;
float power = 1;
int i;
/* Take base and exponent as input*/
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
int expo = exponent;
//To calculate the power of negative exponents
for(i=expo;i<0;i++)
{
power = power/base;
}
//To calculate the power of positive exponents
for(i=expo;i>0;i--)
{
power = power * base;
}
printf("%d ^ %d = %f", base, exponent, power);
return 0;
}
1. In order to calculate the power of a number using a for 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. If the exponent is negative, run a for loop till from i = expo to i less than 0. In each iteration calculate power by dividing power by base and increment the value of i by 1.
4 If the exponent is positive, run a for loop till from i = expo to i greater than 0. In each iteration calculate power by multiplying power by base and decrement the value of i by 1.
5. After coming out of the loop print the value of power.
Example:
1. Consider an example having base = 5 and exponent = 3. Store the value of exponent in variable expo i.e., expo=3. Declare another variable power and assign it to 1.
2. As the value of expo is greater than 0 run the second for loop.
- When the value of base is 3 i.e., i=3 enter the for loop and calculate power so, power = power * base i.e., power = 1*5 = 5 and decrement the value of i by 1.
- Now, i=2, again enter the for loop and repeat the same process. So, power = 5*5 = 25 and i = i– = 1.
- Now, i = 1 again enter the for loop and repeat the same process. So, power = 25*5 = 125 and i = i– = 0.
- Now, i = 0, check the condition in for loop, as it is not greater than 0 come out of the loop.
3. Print the value of power.
Time Complexity: O(n)
The above program for finding power of a number has a time complexity of O(n), where n is the value assigned to the exponent.
Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Testcase 1: In this case, both base and exponent values are positive.
Enter base: 2 Enter exponent: 5 2 ^ 5 = 32.000000
Testcase 2: In this case, exponent value is “0”.
Enter base: 12 Enter exponent: 0 12 ^ 0 = 1.000000
Testcase 3: In this case, base value is negative and exponent value is positive.
Enter base: -5 Enter exponent: 3 -5 ^ 3 = -125.000000
Testcase 4: In this case, base value is positive and exponent value is negative.
Enter base: 4 Enter exponent: -2 4 ^ -2 = 0.062500
In this approach, we find the power of a number using recursion.
Here is source code of the C Program to find the power of a number using recursion. 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 recursion
*/
#include <stdio.h>
float power (int, int);
float negpower (int,int);
int main()
{
int pow, num;
float result;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter it's power: ");
scanf("%d", &pow);
if (pow<0)
result = 1/negpower(num, pow);
else
result = power(num, pow);
printf("%d^%d is %f", num, pow, result);
return 0;
}
//function to find power if base is negative
float negpower(int num,int pow)
{
if (pow<0)
{
return (num * negpower(num, pow + 1));
}
return 1;
}
//function to find power if base is positive
float power (int num, int pow)
{
if (pow)
{
return (num * power(num, pow - 1));
}
return 1;
}
1. In order to calculate the power of a number we have to take two inputs from user, pow and num, and to store the result declare another variable result.
2. If the value of pow is less than 0 then return the function negpower() else return the function power().
3. The value returned by the the function negpower() is divided by 1 in order to get the power of negative exponent.
4. The power() function returns the value for the positive exponent and the value returned is stored in result variable.
5. Finally print the result.
Example:
Consider an example having num=5 and pow=3. As pow is greater than 0 enter the function power(). In the power() function check if pow is greater than 0, as the condition is true enter inside if statement and return, return (num * power(num, pow – 1)); i.e., 5*power(5,2) so, this will again recursively call the function power with num = 5 and pow = 2.
Again, check the if condition pow = 2 which is greater than 0 therefore enter the if condition and return 5*5*power(5,1). Now value of num = 5 and pow = 1. Again check the if condition 1>0 so, return 5*5*5*pow(5,0). Now, pow = 0 which is not greater than 0, therefore if condition becomes false so we return 1 i.e., 5*5*5*1 = 125. The value returned by the power() function gets stored in variable result, i.e., result = 125. Now, print the result.
Time Complexity: O(n)
The above program for finding power of a number has a time complexity of O(n), where n is the value assigned to the variable pow.
Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Testcase 1: In this case, both base and exponent values are positive. (i.e. base=2 and exponent=5)
Enter a number: 2 Enter it's power: 5 2^5 is 32.000000
Testcase 2: In this case, exponent value is “0”. (i.e. base=12 and exponent=0)
Enter a number: 12 Enter it's power: 0 12^0 is 1.000000
Testcase 3: In this case, base value is negative and exponent value is positive. (i.e. base=-5 and exponent=3)
Enter a number: -5 Enter it's power: 3 -5^3 is -125.000000
Testcase 4: In this case, base value is positive and exponent value is negative. (i.e. base=4 and exponent=-2)
Enter a number: 4 Enter it's power: -2 4^-2 is 0.062500
In this approach, we find the power of a number using pow() function.
Here is source code of the C Program to find the power of a number using pow() function. 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 pow() function
*/
#include <math.h> //header file for pow() function.
#include <stdio.h>
void main()
{
int base, exp;
float res;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exp);
//pow() function to calculate power of a function
res = pow(base, exp);
printf("%d ^ %d = %f", base, exp, res);
}
1. Here, we are calculating power of a number using pow() function.
2. The pow() function is an inbuilt c function included in header file “math.h”.
3. It takes base and exponent as input and return us the power of the number.
4. The value returned by the pow() function is stored in the variable res and then the output is printed.
Example:
Consider, an example with base = 2 and exponent = 5. The value 2 and 5 is passed in pow() function and pow function returns us the power of the function. The value returned by pow() function is then stored in the variable res, i.e., res=32 and is printed as output.
Time Complexity: O(n)
The above program for finding power of a number has a time complexity of O(n). This is because internally pow() function works recursively to generate the output.
Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Output1: Enter base: 2 Enter exponent: 5 2 ^ 5 = 32.0000000 Output2: Enter base: 12 Enter exponent: 0 12 ^ 0 = 1.000000 Output3: Enter base: -5 Enter exponent: 3 -5 ^ 3 = -125.000000 Output4: Enter base: 4 Enter exponent: -2 4 ^ -2 = 0.062500
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Apply for Computer Science Internship
- Buy Computer Science Books
- Practice Computer Science MCQs
- Buy C Books