×

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power function is pow(a,b).

The function gives the result as a^b. To use this function, we have to include the math.h header file. We have to pass two values as the argument in the function. It considers the first value as the base and the second as the exponent.

We can pass integer, float and double into this function. We can’t pass character values. By default, it takes the double value and returns a double value.

Ex:-

pow(2,3)

It gives the result as 8

Example program

// program for finding powers of the given number using pow() function in static way

#include<stdio.h>
#include<math.h>
Int main()
{
	int base= 2;
	int exponent= 5;
	int c;
	c= pow(2,5); // Function calling
	// printing the power of 2
	printf(“The fifth power of 2 is= %d ”,c);
	return 0;
}

Output :

test case 1
The fifth power of 2 is = 32

// program for finding power of the given number using pow() function in Dynamic way

#include<stdio.h>
#include<math.h>
int main()
{
int base, exponent, c;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent);
c=pow(base, exponent);// function calling and passing base and exponent values
// printing the power of base
printf(“\n %d power of %d is =”, exponent,base,c);
return 0;
}

Output :-

test case 1
Enter the base value: 3
Enter the exponent value: 2
2 power of 3 is= 9

//program to find power with out using pow() function in non recursive method

#include<stdio.h>
#include<math.h>
int main()
{
int base, exponent, power, i;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent); // scanning the exponent value
for( i=1; i<=exponent; i++ )
{
	Power = power * base ;
}
printf(“ %d power of %d is = %d ”, exponent, base, power );  
return 0;
}

Output :-

test case 1
Enter the base value: 10
Enter the exponent value: 2
2 power of 10 is= 100

//program to find power with out using pow() function in recursive method

#include<stdio.h>
#include<math.h>
int calculate ( int a, int b );// function declaration
int calculate (int a, int b )   //function body
{
	int i;
	if( b = = 0 )
	{
		return 1;
	}
	Else
	{
		Return ( a * calculate (a, b-1));
	}
}
int main()
{
int base, exponent, power ;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent); // scanning the exponent value
power = calculate( base, exponent ); // function calling 
printf(“ %d power of %d is = %d ”, exponent, base, power );  
return 0;
}

Output :-

test case 1
Enter the base value: 5
Enter the exponent value: 3
3 power of  is= 125

Advantages of pow() function:-

1 . Complexity of the code gets reduced
2 . The code will be simple and easy to understand


Related Topics

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.