×

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

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 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.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.