 # C Program to calculate x^n - program to compute pow(x,n)

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this c program, we have to calculate 'x raise to n' where 'x' and 'n' are given by users in a short program to compute pow(x,n). For example, if 'x' is equal to 2 and 'n' is 3, then the result will be 8. Here we are using the recursive function to do the calculation.

## What is a recursive function?

A recursive function is a function which calls itself until the exit condition met. Hence the same functions are called different times by themselves. In this c program, we use a recursive function to find the power, 'x' raise to 'n'. in this function we do the calculation separately and return " (pow(power(x,n/2),2));" for even and return "(x*power(x, n-1))".

## What is the pow() function?

This program uses a power function defined in the math library. The pow() is used to calculate the power raised of a number. The syntax of the pow() function is given below.

double pow(double a double b)

The pow() function calculates the Power of 'a' raise to 'b' and returns the result.

## What is the syntax of the If else statement?

``````
if (testExpression) {

// codes inside the body of if

} else {

// codes inside the body of else

}
```
```

If the test expression is True, We execute the code inside the `if` condition, and we skip the else part. But if the test expression is False, we will do the reverse as we will execute the else function, and we ignore the if condition part.

### ALGORITHM

STEP 1: Include the header files to use the built-in Functions in the C program.

STEP 2: Declare the variables 'x, n, xpown' as a `Long integer`.

STEP 3: Declare the function long int power(int x, int n).

STEP 4: Read the values of X and N from the user.

STEP 5: Call the Function xpown = power (x,n).

STEP 6: Display X to the Power N is xpown.

#### Function long int power(int x, int n)

STEP 1: Check if n==1 if true then return(x).

STEP 2: Else check if n%2 == 0 then return (pow(power(x,n/2),2))  else do step 3.

STEP 3: Return (x*power(x, n-1)).

## C Source Code

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

void main() {
long int x, n, xpown;
long int power(int x, int n);
printf("Enter the values of X and N\n"); /* accepts the user input */
scanf("%ld %ld", & x, & n);
xpown = power(x, n);
printf("X to the power N = %ld\n");
}
/*Recursive function to compute the X to power N*/
long int power(int x, int n) {
if (n == 1)
return (x);
else if (n % 2 == 0)
return (pow(power(x, n / 2), 2)); /*use this when the n i even*/
else
return (x * power(x, n - 1)); /* if n is odd*/
}```
```

## OUTPUT

```Enter the values of X and N
2 5
X to the power N = 32

RUN2

Enter the values offX and N
4 4
X to the power N ==256

RUN3

Enter the values of X and N
5 2
X to the power N = 25

RUN4

Enter the values of X and N
10 5
X to the power N = 100000```
VIEW ALL
VIEW ALL
VIEW ALL