# C Program to Find Power of a Number

«
»

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.

Problem Description

Write a C Program which calculates the power of any number, by taking the values of base and exponent from the user.

Problem Solution

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.

Expected Input and Output

1. When base and exponent both are positive

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
```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).```

Method 1: (Using While Loop)

In this approach, we find the power of a number using while loop.

Program/Source Code

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.

1. `/*`
2. ` * C program to find power of any number using while loop`
3. ` */`
4. ` `
5. `#include <stdio.h>`
6. ` `
7. `int main()`
8. `{`
9. `    int base, exponent;`
10. `    float power = 1;`
11. ` `
12. `    /* Take base and exponent as input*/`
13. `    printf("Enter base: ");`
14. `    scanf("%d", &base);`
15. `    printf("Enter exponent: ");`
16. `    scanf("%d", &exponent);`
17. `    int expo = exponent;`
18. ` `
19. `    //to calculate the power of negative exponents`
20. `    while (expo < 0)`
21. `    {`
22. `        power = power/base;`
23. `        expo++;`
24. `    }`
25. ` `
26. `    //To calculate the power of positive exponents`
27. `    while(expo>0)`
28. `    {`
29. `        power = power * base;`
30. `        expo--;`
31. `    }`
32. `    printf("%d ^ %d = %f", base, exponent, power);`
33. ` `
34. `    return 0;`
35. `}`
Program Explanation

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.

Run Time Testcases

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```

Method 2: (Using For Loop)

In this approach, we find the power of a number using for loop.

Program/Source Code

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.

1. `/*`
2. ` * C program to find power of any number using for loop`
3. ` */`
4. ` `
5. `#include <stdio.h>`
6. ` `
7. `int main()`
8. `{`
9. `    int base, exponent;`
10. `    float power = 1;`
11. `    int i;`
12. ` `
13. `    /* Take base and exponent as input*/`
14. `    printf("Enter base: ");`
15. `    scanf("%d", &base);`
16. `    printf("Enter exponent: ");`
17. `    scanf("%d", &exponent);`
18. `    int expo = exponent;`
19. ` `
20. `    //To calculate the power of negative exponents`
21. `    for(i=expo;i<0;i++)`
22. `    {`
23. `        power = power/base;`
24. `    }`
25. ` `
26. `    //To calculate the power of positive exponents`
27. `    for(i=expo;i>0;i--)`
28. `    {`
29. `        power = power * base;`
30. `    }`
31. `    printf("%d ^ %d = %f", base, exponent, power);`
32. ` `
33. `    return 0;`
34. `}`
Program Explanation

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.

Run Time Testcases

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```

Method 3: (Using Recursion)

In this approach, we find the power of a number using recursion.

Program/Source Code

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.

1. `/*`
2. ` * C program to find power of any number using recursion`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `float power (int, int);`
7. `float negpower (int,int);`
8. ` `
9. `int main()`
10. `{`
11. `    int pow, num;`
12. `    float result;`
13. ` `
14. `    printf("Enter a number: ");`
15. `    scanf("%d", &num);`
16. `    printf("Enter it's power: ");`
17. `    scanf("%d", &pow);`
18. `    if (pow<0)`
19. `        result = 1/negpower(num, pow);`
20. `    else`
21. `        result = power(num, pow);`
22. `    printf("%d^%d is %f", num, pow, result);`
23. `    return 0;`
24. `}`
25. ` `
26. `//function to find power if base is negative`
27. `float negpower(int num,int pow)`
28. `{`
29. `    if (pow<0)`
30. `    {`
31. `        return (num * negpower(num, pow + 1));`
32. `    }`
33. `    return 1;`
34. `}`
35. ` `
36. `//function to find power if base is positive`
37. `float power (int num, int pow)`
38. `{`
39. `    if (pow)`
40. `    {`
41. `        return (num * power(num, pow - 1));`
42. `    }`
43. `    return 1;`
44. `}`
Program Explanation

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.

Run Time Testcases

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```

Method 4: (Using pow() Function)

In this approach, we find the power of a number using pow() function.

Program/Source Code

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.

1. `/*`
2. ` * C program to find power of any number using pow() function`
3. ` */`
4. `#include <math.h>   //header file for pow() function.`
5. `#include <stdio.h>`
6. ` `
7. `void main()`
8. `{`
9. `    int base, exp;`
10. `    float res;`
11. `    printf("Enter base: ");`
12. `    scanf("%d", &base);`
13. `    printf("Enter exponent: ");`
14. `    scanf("%d", &exp);`
15. ` `
16. `    //pow() function to calculate power of a function`
17. `    res = pow(base, exp);`
18. ` `
19. `    printf("%d ^ %d = %f", base, exp, res);`
20. `}`
Program Explanation

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.

Program Output:
```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”. 