×

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

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

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.