Power Function in C

Hello Coders! Finding the power of a number is a very common operation in programming logic. In this article, we will understand how to write a program using Power Function In C to calculate the power of a number.

We use the pow() function to calculate the power raised to the base value. We use the pow() function to perform various mathematical calculations. Let’s understand this function in detail.

Introduction

Let us start with understanding how to write a program to calculate the power of a number. Logic For Power Multiplication

33 = 3*3*3 = 27

To find the 3^3, you need to multiply 3 thrice. We can achieve this using the for loop.

For above example, base = 3, exponent = 3.

The function pow() is used to calculate the power raised to the base value. It takes two arguments. This function return the power raised to the base value. It is declared in the “math.h” header file.

Here is the syntax of pow() in C language,

double pow(double val1, double val2);

Here, val1 − The base value whose power is to be calculated and val2 − The power value.

Return Value: This function returns the result of raising x to the power y.

The pow() function takes ‘double’ as the argument and returns a ‘double’ value. This function does not always work for integers. One such example is pow(6, 2). When assigned to an integer, it outputs 35 on some compilers and works fine for some other compilers. But pow(6, 2) without any assignment to an integer gives output 36.

This is because 62 (i.e. 36) might be stored as 35.9999999 or 36.0000000001 because the return type is double. When assigned to int, 36.0000000001 becomes 36 but 35.9999999 will give output 35.

To overcome this and output the accurate answer in integer format, we can add 0.5 to the result and typecast it to int e.g (int)(pow(6, 2)+0.5) will give the correct answer(36, in the above example), irrespective of the compiler.

This technique only works if the exponent is a positive integer. If you want to find the power of a number where an exponent is a real number, you need to use the pow() function.

Example#1

Before we move forward to using the pow() function, it is important to understand how the pow() function works. Lets dive into an example:

#include <stdio.h>
#include <string.h>

int Pow(int X, int Y) {

	int power = 1, i;

	for (i = 1; i <= Y; ++i) {
		power = power * X;
	}

	return power;

}

int main() {

	long long int base, exponent;

	printf("Enter Base: ");
	scanf("%d", &base);

	printf("Enter Power: ");
	scanf("%d", &exponent);

	printf("%d ^ %d = %d", base, exponent, Pow(base, exponent));

}

Suppose you need to find the value of Xy. The simplest way is to use a loop and multiply X, Y times.

Steps:

  1. Let the input be X is the base and Y is the exponent.
  2. Initialize power = 1.
  3. Repeat the following steps Y times using a loop( power = power * X)
  4. The final answer is stored in power.

Output:

Enter Base: 5
Enter Power: 3
5 ^ 3 = 125

The time complexity of the above code is O(N), where N is the exponent and the space complexity is O(1).

Example#2

Now, we can proceed to understand and use the power (pow()) function.

#include <stdio.h>
#include <math.h>

int main () {
   printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));

   printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
   
   return(0);
}

As you can see in the above code, we have included math.h file so that we can use the functions of the math library. After that, in the main class, we have declared three double data type parameters. In those parameters, one is the base value and other is exponent value.

Here, the output will be as follows:

Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324

On some online compilers, the following error may occur.

undefined reference to `pow'
error: ld returned 1 exit status

The above error occurs because we have added “math.h” header file, but haven’t linked the program to the following math library.

libm.a

Link the program with the above library, so that the call to function pow() is resolved.

Example#3

In this example we have used typecasting in order to get integer value as the result. Kindly, go through the given code-


#include <math.h>
#include <stdio.h>
int main()
{
	int a;

	// Using typecasting for
	// integer result
	a = (int)(pow(5, 2) + 0.5);
	printf("%d", a);

	return 0;
}

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float and vice versa. In this example we have converted float to int using typecasting.

Here, the output will be a perfect integer value-

25

Example#4

This example will clear all your doubts on pow() function. Kindly try and understand each and every step from the given code.

#include <stdio.h> // Header files
#include <math.h>
int main()
{
int output ;
double output_1 , output_2 , output_3 , output_4 ;
output = pow ( 1 , 2 ) ;
printf ( " \n The output of %d to the power %d = %d " , 1 , 2 , output ) ;
output_1 = pow ( 2 , 3 ) ;
printf ( " \n The output_1 of %d to the power %d = %f " , 2 , 3 , output_1 ) ;
output_2 = pow ( 3 , 4 ) ;
printf ( " \n The output_2 of %d to the power %d = %f " , 3 , 4 , output_2 ) ;
output_3 = pow ( -4 , 5 ) ;
printf ( " \n The output_3 of %d to the power %d = %f " , -4 , 5 , output_3 ) ;
output_4 = pow ( 5 , -6 ) ;
printf ( " \n The output_4 of %d to the power %d = %f " , 5 , -6 , output_4 ) ;
return 0 ;
}

In the above code, you can see we have declared five temporary variables to calculate the power named as “output, output_1, output_2, output_3, output_4”. After declaration, we are calling the power function for each and every temporary variable we have declared bypassing the argument value to the power function. We are also displaying the result of power values side by side using a print method of c. As in the output you can see all the results with raise power to the specified base value.

Output:

The output of 1 to the power 2=1
The output_1 of 2 to the power 3=8.000000
The output_2 of 3 to the power 4=81.000000
The output_3 of -4 to the power 5=-1024.000000
The output_4 of 5 to the power -6=0.000064

Questions on Pow()

After going through so many examples, we hope you would be able to answer the following questions on power (pow()) function-

Q1. Library function pow() belongs to which header file?

(A) mathio.h
(B) math.h
(C) square.h
(D) stdio.h

Answer: B. math.h

Q2. Which of the following is the correct syntax of pow()?

  1. double pow(int val1, int val2)
  2. double pow(double val1, double val2)
  3. int pow(double val1, double val2);
  4. double pow(val1,val2);

Answer: 2. double pow(double val1, double val2)

Q3. Calculate the output of the following code-

#include<stdio.h>
#include<math.h>
int main() {
   double x = 5.5;
   double y = 4.0;
   double p;
   p = pow(x, y);
   printf("The value : %lf", p);
   return 0;
}

Q4. Calculate the output of the following code-

#include <stdio.h>
#include <math.h>

int main () {
   printf("Value 7.0 ^ 7 = %lf\n", pow(7.0, 7));

   printf("Value 5.7 ^ 3.98 = %lf", pow(5.7, 3.98));
   
   return(0);
}

Q5. The output of -4 to the power 5 is?

  1. -1024.00000
  2. 1024.00000
  3. 102.40000
  4. 1024

Answer: 1. -1024.00000

Conclusion

Power function in C plays a crucial role in the complex as well as large mathematic calculations in quick time duration.  Write the syntax and pass the value as arguments and rest will be done by power function irrespective of values as it can calculate positive and negative numbers.

Hope you find this post helpful. We have discussed the Power Function in C and how to implement it. We hope that you found it easy to understand.

Stay tuned for more posts and updates. Until then, Happy Coding!

Do check out our post on Ceil Function https://geekstocode.com/ceil-function-in-c/

Leave a Comment

Your email address will not be published. Required fields are marked *