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

- 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`

. - 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. - In the
`main`

function, we declare variables`x`

,`n`

, and`result`

. - We prompt the user to enter the base value (x) and read it using
`scanf`

with the`%lf`

format specifier for a double. - We prompt the user to enter the exponent value (n) and read it using
`scanf`

with the`%d`

format specifier for an integer. - We call the
`calculatePower`

function, passing`x`

and`n`

as arguments, and assign the returned value to the`result`

variable. - Finally, we display the result using
`printf`

with the`%.2lf`

format specifier to display the result with two decimal places.

## Leave a Review