Program to find Power of Number in C Language
- Program Introduction:
- What are the Base, Exponent, and Power of a Number?
- Method 1: Calculate the power of Number in C language using loops Algorithm:
- Method 1.1: Calculate the power of Number in C language using While Loop:
- Method 1.2: Calculate the power of Number in C language using for Loop:
- Method 2: Calculate the Power of Number in C using pow function:
- Related Articles:
Program Introduction:
Write a program to calculate the Power of Number in C programming. The program should accept two numbers from the user, one is base and another is exponent and calculate the power.
What are the Base, Exponent, and Power of a Number?
The power is equal to the repeated multiplication of the base, exponent times.
If the base is 2 and exponent is 5. Then power is equal to 2 multiplied 5 times – 2 * 2 * 2 * 2 * 2, which (Power of number) is 32.
We can write the Power of Number program manually by using the C Loops like While, for loops. and we also have a built-in C function called pow which is used to calculate the power of the number. So we can simply use the pow function to calculate the power. Let’s look into these two cases.
Excepted Inputs and Outputs:
Let’s quickly look at the expected inputs and outputs of the program
Input:
1 2 |
Enter base number: 2 Enter exponent number: 4 |
Output:
1 |
Result is : 16.000000 |
Program Pre-Requisites:
It is recommended to have the basic knowledge of the C Loops like while loop and for loop and goto statement. As we are going to use them in this program.
As specified earlier, We are going to look at different methods to calculate the power of number. Let’s start.
Method 1: Calculate the power of Number in C language using loops Algorithm:
- Take the base and exponent values from the user and store them in variables base and exponent
- The base and exponent should be positive numbers, So check if the user provided negative numbers for any of them ( base or exponent values). If a negative number is provided, Display the error message Invalid Input, Please try again and stop the program.
- If both base and exponent are positive, We are going to continue to the next step.
- The Power is the repeated multiplication of the base, exponent times. So we use the loop to multiply the base, exponent times.
- We use a res variables initialized to 1.0 and shall be used to maintain the power so far.
- At Each iteration
- We multiply the base with res and add it to res. res = res * base;
- Then we decrement the exponent by one. exponent--;
- This loop continues until the exponent became zero ( exponent > 0 )
- Once the loop is terminated, The res variable contains the result (Power of number).
Method 1.1: Calculate the power of Number in C language using While Loop:
Here is the power of the number program using the while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/*     Program: Power of number program manual method     Author: Sillycodes.com */ #include<stdio.h> int main() {     double base;     int exponent;      // Take the base and exponent values from user     printf("Enter base number: ");     scanf("%lf", &base);     printf("Enter exponent number: ");     scanf("%d", &exponent);      // Check if any of the 'base' or 'exponent' is a negative number,     // if negaive number, display error and exit the program     if(base <= 0 || exponent <= 0) {         printf("Invalid Input, Please try again \n");         return 0;     }      // our result is stored in 'res' variable     long double res = 1.0;      // The power of base and exponent is equal to ( base multiplied exponent times)     // So loop until the exponent became zero.     while(exponent > 0) {         res = res * base;  // res *= base;         exponent--;     }      // print the results     printf("Result is : %Lf \n", res);         return 0; } |
We have used the doubledatatype for base variable and integer datatype for the exponent variable(for simplicity).
The res (i.e power) is a long doublevariable to hold large values.
Program Output:
Compile and run the program.
1 2 3 4 5 6 7 8 9 10 11 |
$ gcc power-of-number.c $ ./a.out Enter base number: 2 Enter exponent number: 10 Result is : 1024.000000 Â $ ./a.out Enter base number: 8 Enter exponent number: 3 Result is : 512.000000 $ |
As you can see we are getting the expected results.
Let’s try double value for base variable
1 2 3 4 5 6 |
// try double value for base $ ./a.out Enter base number: 2.3 Enter exponent number: 10 Result is : 4142.651121 $ |
What happens if the user provides Negative numbers ( if base is negative or exponent is negative or both are negative)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// If exponent is negative number $ ./a.out Enter base number: 10 Enter exponent number: -34.5 Invalid Input, Please try again  // if base is negative number $ ./a.out Enter base number: -7 Enter exponent number: 4 Invalid Input, Please try again  // if both exponent and base are negative numbres $ ./a.out Enter base number: -8 Enter exponent number: -2 Invalid Input, Please try again |
The program is displaying an error notice if the user enters the negative number for neither of base or exponent variables.
Method 1.2: Calculate the power of Number in C language using for Loop:
Let’s rewrite the above program to use the for loop instead of the whileloop.
We can take advantage of the for loops, Initialization, and update steps.
- So we can Initialize the res variable at for loop’s Init step – i.e res = 1.0;
- And we can also update the exponent value at for loops update step. – i.e exponent--
So our for loop looks something like the below.
for(res = 1.0; exponent > 0; exponent – -)
Here is the full program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/*     Program: Power of number program manual method     Author: Sillycodes.com */ #include<stdio.h> int main() {     double base;     int exponent;      // Take the base and exponent values from user     printf("Enter base number: ");     scanf("%lf", &base);     printf("Enter exponent number: ");     scanf("%d", &exponent);      // Check if any of the 'base' or 'exponent' is a negative number,     // if negaive number, display error and exit the program     if(base <= 0 || exponent <= 0) {         printf("Invalid Input, Please try again \n");         return 0;     }      // our result is stored in 'res' variable     long double res;      // The power of base and exponent is equal to ( base multiplied exponent times)     // So loop until the exponent became zero.     for(res = 1.0; exponent > 0; exponent--)     {         res = res * base;  // res *= base;     }      // print the results     printf("Power is : %Lf \n", res);         return 0; } |
Program Output:
Let’s compile and run the program and try different test cases
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$ gcc power-for.c  // Normal case $ ./a.out Enter base number: 4 Enter exponent number: 4 Power is : 256.000000  // base double number $ ./a.out Enter base number: 8.1 Enter exponent number: 3 Power is : 531.441000  // Negative number test $ ./a.out Enter base number: -7 Enter exponent number: 4 Invalid Input, Please try again $ ./a.out Enter base number: 8 Enter exponent number: -123 Invalid Input, Please try again $ ./a.out Enter base number: -5 Enter exponent number: -9 Invalid Input, Please try again  $ ./a.out Enter base number: 10 Enter exponent number: 10 Power is : 10000000000.000000 $ |
Method 2: Calculate the Power of Number in C using pow function:
We can use the inbuilt pow function from the math.h library to calculate the power of a number in C.
Here is the syntax of the pow function.
double pow(double x, double y)
📢 Don’t forget to add the math.h header file to the program.
Let’s use the in-built Pow function to calculate the power of the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/*     Program: Power of number program using built in pow function     Author: Sillycodes.com */ #include<stdio.h> #include<math.h> int main() {      double base, exponent;     // Take the base and exponent values from user     printf("Enter base number: ");     scanf("%lf", &base);     printf("Enter exponent number: ");     scanf("%lf", &exponent);      // Check if any of the 'base' or 'exponent' is negative number,     // if negaive number, display error and exit the program     if(base <= 0 || exponent <= 0) {         printf("Invalid Input, Please try again \n");         return 0;     }      // our result is stored in 'res' variable     long double res;      // let's use the 'pow' function from math.h library.     res = pow(base, exponent);      // print the results     printf("Result is : %Lf \n", res);         return 0; } |
Program Output:
We need to add the -lm option to program during the compilation to avoid the linking error.
As the pow function is part of the math library, We need to add the -lm flag. Here is how we compile the program in Linux
$ gcc power-of-number.c -lm
The above command generates the a.out executable file. Let’s run it and test few test cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$ ./a.out Enter base number: 5 Enter exponent number: 3 Result is : 125.000000 $ ./a.out Enter base number: 7 Enter exponent number: 2 Result is : 49.000000 $ ./a.out Enter base number: 2 Enter exponent number: 10 Result is : 1024.000000 $ ./a.out Enter base number: -3 Enter exponent number: 3 Invalid Input, Please try again $ ./a.out Enter base number: 3.4 Enter exponent number: 3.2 Result is : 50.203331 $ ./a.out Enter base number: 9 Enter exponent number: -23 Invalid Input, Please try again $ ./a.out Enter base number: -54 Enter exponent number: -2 Invalid Input, Please try again $ ./a.out Enter base number: 4 Enter exponent number: 30 Result is : 1152921504606846976.000000 $ |
As you can see we are getting the expected results.
3 Responses
[…] C Program to Calculate the Power of Number […]
[…] of rem with nDigits(Number of digits in n) – i.e power(rem, nDigits);. We are using the Power of Number Program in C to calculate the power instead of the pow built-in function. Then Update the arm […]
[…] C Program to Calculate the Power of Number […]