Here is a C program to find power of a number (an). We have to take two numbers as input from user(base and exponent) and return baseexponent.
To calculate power of a number we repetitively multiply number(base) exponent times.
For Example
AN = A*A*A*A..... N times A5 = A*A*A*A*ABelow programs will not produce correct result if the value of baseexponent exceeds the range of int data type.
C program to calculate power of number using loop
This program takes base and exponent as input from user using scanf function. To calculate baseexponent, it repetitively multiply base with result inside for loop. Finally, it prints the value of result on screen.
Time complexity of this algorithm is O(n).
#include <stdio.h> int main(){ int base, exponent, counter, result = 1; printf("Enter base and exponent \n"); scanf("%d %d", &base, &exponent); for(counter = 0; counter < exponent; counter++){ result = result * base; } printf("%d^%d = %d", base, exponent, result); return 0; }Output
Enter base and exponent 2 8 2^8 = 256
C program to find power of a number using divide and conquer
Let power(a, b) means ab. If b is even : power(a, b) = power(a, b/2)*power(a, b/2); if b is odd : power(a, b) = power(a, b/2)*power(a, b/2)*a; For example: 28 = 24 * 24; 29 = 24 * 24 * 2; Once we calculated power(a, b/2), we don't have to calculate it again. We can simply square it. Let square(x) function returns the value of x2. Now, If b is even : power(a, b) = square(power(a, b/2)); If b is odd : power(a, b) = square(power(a, b/2))*a;Time complexity of this algorithm is O(logn).
This uses a user defined function power, that implement above mentioned recursive algorithm to find the power of a number. This program can only be used to calculate the power of integers, we cannot use it to calculate power of floating point numbers. If we want to calculate power of floating point number the we should use pow function of math.h header file.
#include <stdio.h> int main(){ int base, exponent, counter, result = 1; printf("Enter base and exponent \n"); scanf("%d %d", &base, &exponent); result = power(base, exponent); printf("%d^%d = %d", base, exponent, result); getch(); return 0; } int getSquare(int num){ return num*num; } int power(int base, int exponent){ /* Recursion termination condition, * Anything^0 = 1 */ if(exponent == 0){ return 1; } if(exponent%2 == 1){ /* If exponent is Odd number * a^b = a*(a^(b/2))*(a^(b/2)) */ return base * getSquare(power( base, exponent/2)); } else { /* If exponent is Even number * a^b = (a^(b/2))*(a^(b/2)) */ return getSquare(power( base, exponent/2)); } }Program Output
Enter base and exponent 2 6 2^6 = 64
Enter base and exponent 2 0 2^0 = 1
C program to find power of a number using recursion
We can use recursion to calculate power of a number because it follows recursive sub-problem structure. This approach reduces the problem of finding an to problem of finding an-1 till exponent becomes 0.
Time complexity of this program is O(n).
#include <stdio.h> int main(){ int base, exponent, counter, result = 1; printf("Enter base and exponent \n"); scanf("%d %d", &base, &exponent); result = getPower(base, exponent); printf("%d^%d = %d", base, exponent, result); return 0; } int getPower(int base, int exponent){ if(exponent == 0){ return 1; } return base * getPower(base, exponent - 1); }Program Output
Enter base and exponent 2 8 2^8 = 256
Related Topics