Learn Programming and technology with ease @ developerpublish.com

HomeC ProgrammingC Program to Calculate Pow (x,n)

# C Program to Calculate Pow (x,n)

In this C program, we will calculate the power of a number using the exponentiation operator. We will take the base value â€˜xâ€™ and the exponent value â€˜nâ€™ from the user, and then calculate the result of x raised to the power of n.

## Problem statement

Write a C program to calculate the power of a number using the exponentiation operator.

## C Program to Calculate Pow (x,n)

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

double calculatePower(double x, int n);

int main()
{
double x;
int n;
double result;

printf("Enter the base value (x): ");
scanf("%lf", &x);

printf("Enter the exponent value (n): ");
scanf("%d", &n);

result = calculatePower(x, n);

printf("Result: %.2lf\n", result);

return 0;
}

double calculatePower(double x, int n)
{
return pow(x, n);
}```

## How it works

1. We include the necessary header files `stdio.h` and `math.h`. The `stdio.h` header file provides input/output functions, and the `math.h` header file provides mathematical functions such as `pow`.
2. We declare a function `calculatePower` that takes a double value `x` as the base and an integer value `n` as the exponent. This function uses the `pow` function from `math.h` to calculate and return the result of x raised to the power of n.
3. In the `main` function, we declare variables `x`, `n`, and `result`.
4. We prompt the user to enter the base value (x) and read it using `scanf` with the `%lf` format specifier for a double.
5. We prompt the user to enter the exponent value (n) and read it using `scanf` with the `%d` format specifier for an integer.
6. We call the `calculatePower` function, passing `x` and `n` as arguments, and assign the returned value to the `result` variable.
7. Finally, we display the result using `printf` with the `%.2lf` format specifier to display the result with two decimal places.

## Input / Output

Share:

### You May Also Like

#### C Program to Find Volume and Surface Area of Sphere

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...

#### C Program to Convert Roman Number to Decimal Number

This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...

#### C Program to Calculate the Value of sin(x)

This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...