Power Function In C

Syntax

Example

int pow (int x, int y);

The pow function takes two integer parameters to calculate the result of raising a base value (x) to the power of an exponent (y).

Parameters:

  • x: The x variable represents the base value, whose power is to be calculated.
  • y: The y variable represents the power or exponent value.
  • Program to find the power of an integer number using the pow function

Let's explore an illustration demonstrating how to calculate the exponentiation of an integer using the pow function within the C programming language.

Program2.c

Example

#include <stdio.h>
#include <math.h>
int main ()
{
// declare base and exp variable 
int base, exp;
int result; // store the result
printf (" Enter the base value from the user: ");
scanf (" %d", &base); // get base from user
printf (" Enter the power value for raising the power of base: ");
scanf (" %d", &exp); // get exponent from user

// use pow() function to pass the base and the exp value as arguments
result = pow ( base, exp);
printf (" %d to the power of %d is = %d ", base, exp, result);
return 0;
}

Output

Output

Enter the base value from the user: 7
 Enter the power value for raising the power of base: 5
 7 to the power of 5 is = 16807

Program to calculate the power of double data value using the pow function

Let's use an illustration to showcase the pow function with double data types in the C programming language.

Program2.c

Example

#include <stdio.h>
#include <math.h>
int main ()
{
// declare base and exp variable 
double base, exp;
double result; // store the result
printf (" Enter the base value: ");
scanf (" %lf", &base); // get base from user
printf (" Enter the power value: ");
scanf (" %lf", &exp); // get exponent from user

// use pow() function to pass the base and the exp value as arguments
result = pow ( base, exp);
printf (" %.1lf to the power of %.1lf is = %.2lf ", base, exp, result);
return 0;
}

Output

Output

Enter the base value from the user: 2.9
 Enter the power value for raising the power of base: 8.3
 2.9 to the power of 8.3 is = 6884.99

Program to get the power of a number using a user-defined function

Let's explore an illustration for defining a custom function to calculate the exponent of a number in the C programming language.

Program1.c

Example

#include <stdio.h>
#include <math.h>
int main ()
{
	// declare local variable
	long int base, exp;	
	printf (" Enter the base value: ");
	scanf (" %d", &base); // take a number from user
	
	printf (" Enter the power value: ");
	scanf (" %d", &exp); // take a number from user
	
	// use pow() function to pass the base and exp variable
	printf (" %d to the power %d is = %d ", base, exp, pow_num (base, exp));
}

// definition of the function
int pow_num (int x, int y)
{
	int power = 1, i; // declare variables
	for (i = 1; i <= y; ++i)
	{
		power = power * x;
		
	}
	return power;
}

Output

Output

Enter the base value: 25
 Enter the power value: 6
 25 to the power 6 is = 244140625

Program to get the power of a number using the recursion function and pow function

Let's explore an illustration to determine the exponentiation of a value by employing recursive function and the pow function in the C programming language.

Program.c

Example

#include <stdio.h>
#include <math.h>
int main ()
{
	// declare local variable
	long int base, exp;	
	printf (" Enter the base value from the user ");
	scanf (" %d", &base); // take a number from user
	
	printf (" Enter the power value from the user ");
	scanf (" %d", &exp); // take a number from user
	
	// use pow() function to pass the base and exp variable
	printf (" %d to the power of %d is = %d ", base, exp, pow_num (base, exp));
}

// definition of the function
int pow_num (int x, int y)
{
	if ( y == 0)
	{
		return 1;
		}	
	else
		return pow_num (x, y - 1) * x;	
}

Output

Output

Enter the base value from the user 5
 Enter the power value from the user 6
 5 to the power of 6 is = 15625

Conclusion

A power function is employed to determine the exponentiation of various numeric data types such as integers, floating-point, and double precision using the pow function. This function requires two parameters: the base value and the exponent value. The base number signifies the value to be raised to a certain power, while the exponent specifies the degree to which the base value is raised.

Input Required

This code uses input(). Please provide values below: