Table of contents
1.
Introduction
2.
Problem Statement
2.1.
About POW()
2.2.
Implementation 
2.2.1.
Try it on the C compiler for a better understanding.
2.2.2.
Program Explanation
2.2.3.
Complexity Analysis
3.
Frequently Asked Questions
3.1.
What type does math POW return?
3.2.
What is the output of pow( 5, 4)?
3.3.
What is the output of pow( 2, 3)?
3.4.
How many parameters does the pow() function take?
3.5.
Explain how the built-in function pow () differs from the Math.pow () function with an example.
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

C program to calculate x^n using pow function

Author Aditya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The library function pow() is used to determine the power of any base; it is defined in the math.h header file. 

We'll read X as the base and N as the power in this article, then calculate the result (XN - X to the power of N).

So, let’s get started: 

Also Read, Binary to Hex Converter and C Static Function.

Problem Statement

Write a C program to input any two numbers x and n and find their power(xn) using the inbuilt pow() function.

About POW()

The pow() function calculates a number's power. The pow() function accepts two arguments (base and power) and returns the power raised to the base number.

In the math.h header file, the pow() function is defined.

Let's understand with an example:

Implementation 

/*  C program to find power of a number ( x^n ) using pow function  */
#include <stdio.h>
#include <math.h>

void Solve()
{
    int x,n;
    int ans;
    printf("Enter the value of base: \n");
    scanf("%d",&x);
    printf("\nEnter the value of power: \n");
    scanf("%d",&n);
    
    ans =pow((double)x,n);
    
    printf("\n%d to the power of %d is= %d", x,n, ans);
}

int main()
{
    Solve();
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

 

 

Try it on the C compiler for a better understanding.

Program Explanation

The library function pow(), defined in the “math.h” header file, is used to compute mathematical functions in this C application. Using the 'x' and 'n' variables, we read two integer values and feed them to the power() function to compute XN.

Complexity Analysis

Time Complexity: O(log(N))

Space Complexity: O(1)

You can also read about dynamic array in c and  Tribonacci Series.

Frequently Asked Questions

What type does math POW return?

It returns the value when the first parameter raised to the power of the second argument is returned by pow(). The pow() method returns a double value.

What is the output of pow( 5, 4)?

The output of pow(5, 4) is 625.

What is the output of pow( 2, 3)?

The output of pow(2, 3) is 8.

How many parameters does the pow() function take?

The pow() function has three parameters: x, which is a base, n  is the exponent of a number x. z (optional) - a number that will be used to calculate modulus.

Explain how the built-in function pow () differs from the Math.pow () function with an example.

The built-in pow(x,y [,z]) function provides a third parameter called modulo that can be used to compute x raised to the power of y and optionally do a modulo (percent z) on the result. (xy) % z is the same as the mathematical operation. The math. pow() function, on the other hand, does not support modulo.

Conclusion

In this article, we had seen the implementation of the C program to calculate xn using the power function. We had also seen the output of the written program on some random input.

If you want to learn more about C programs, visit the given links below:

Ternary Operator in C

Transpose of a Matrix in C

Bubble Sort Program in C

C Program to count the length of string without using any library function

Armstrong Number in C

Short int in C Programming

Conditional Operator in C

Getch in C

Program to convert Fahrenheit to Celsius

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, and System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass