EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Power Function in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Power Function in C

Power Function in C

Introduction to Power Function in C

Power Function is a normal function in C which help in calculating the power raised to the base value. It is declared under the “ math.h ”  header file in C library. If we have two number in which one is the base number while the other one is an exponent. POW() function is used to calculate the value a raised to the power b this means ab. For instance, to calculate the value of ab,Pow() function is used. It is used to compute the power of the numbers as it takes two arguments power value and base value.

Syntax

double pow ( double base, doube exponent);

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Parameters used in Power Functions in C

Below are the parameters mentioned:

BASE: In the above syntax, base is the floating point base value where as,

EXPONENT: In the above syntax, exponent is the floating point value.

Code:

#include<stdio.h>
#include<math.h>
int main() {
double a = 7.5 ;//  assigning values to the declared variables a,b.
double b = 3.0 ;
double power ;  // this variable will store the value of power
power = pow (a, b) ; // calling power function for calculation
printf ( " The value of a raised to the power b is : %lf " , power );
return 0 ;
}

Output:

floating point value

Explanation: In the above code, you can see we have declared 3 variables a, b and power. After that we are calling the power function to calculate the value of a raised to the power b.

Examples to Implement Power Function in C

Below are the examples mentioned:

Example #1

Here is the C code to demonstrate the working of Power Functions:

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Code: 

#include <stdio.h>
#include <math.h>
int main()
{
double base, value_exponent, value_power ;
printf ( " Please enter the base value to calculate the power: " ) ;
scanf ( " %lf " , &base ) ;
printf ( " \n Please enter the exponent value to calculate the power: " ) ;
scanf ( " %lf " , &value_exponent ) ;
value_power = pow ( base , value_exponent ) ;
printf ( " %.1lf ^ %.1lf = %.2lf " , base , value_exponent , value_power ) ;
return 0 ;
}

Output:

value power

Explanation: As you can see in the above code, we have included math.h file so that we can use the functions of the math library. After that, in the main class, we have declared three double data type parameters. In those parameters, one is the base value, other is exponent value, and the last one is value_power to store the calculated value. Similarly, by using input and output classes we are taking input values from the users and then calculating the Power of the numbers using a Power function.

Example #2

Here is another C code to demonstrate the working of Power Functions:

Code:

#include <stdio.h> // Header files
#include <math.h>
int main ( int argument , const char * argv[] )
{
double temp1 , temp2 ;  // temporary variables for taking input from the user
double output ; //temporary variables for output
temp1 = 6 ; // assigning values to the temporary variables
temp2 = 4 ;
output = pow ( temp1 , temp2 ) ;
// temp1 raised to the power of  temp2 calculation
// printf to display the final result of calculated power
printf ( " The Output of %f raised to the power of %f is %f \n " , temp1 , temp2 , output ) ;
return 0 ;
}

Output:

Power Function in C3

Explanation: As you can see in the above code, we have declared two temporary variables named as temp1, and temp2 and one more output variable are declared to store the output value. After that, we have assigned value 6 to temp1 whereas 4 to temp 2.  Now using the power function we are calculating the value of 6 raised to the power 4 and the displaying the final result through printf method.

Example #3

Here is the C code to demonstrate the working of Power function :

 Code:

#include <stdio.h> // Header files
#include <math.h>
int main()
{
int output ;
double output_1 , output_2 , output_3 , output_4 ;
output = pow ( 1 , 2 ) ;
printf ( " \n The output of %d to the power %d = %d " , 1 , 2 , output ) ;
output_1 = pow ( 2 , 3 ) ;
printf ( " \n The output_1 of %d to the power %d = %f " , 2 , 3 , output_1 ) ;
output_2 = pow ( 3 , 4 ) ;
printf ( " \n The output_2 of %d to the power %d = %f " , 3 , 4 , output_2 ) ;
output_3 = pow ( -4 , 5 ) ;
printf ( " \n The output_3 of %d to the power %d = %f " , -4 , 5 , output_3 ) ;
output_4 = pow ( 5 , -6 ) ;
printf ( " \n The output_4 of %d to the power %d = %f " , 5 , -6 , output_4 ) ;
return 0 ;
}

Output:

Power function

Explanation: In the above code, you can see we have declared five temporary variables to calculate the power named as “output, output_1, output_2, output_3, output_4”. After declaration, we are calling the power function for each and every temporary variable we have declared bypassing the argument value to the power function. We are also displaying the result of power values side by side using a print method of c. As in the output you can see all the results with raise power to the specified base value.

Conclusion

power function in C plays a crucial role in the complex as well as large mathematic calculations in quick time duration.  Write the syntax and pass the value as arguments and rest will be done by power function irrespective of values as it can calculate positive and negative numbers.

Recommended Articles

This is a guide to Power Function in C. Here we discuss an introduction to Power Function in C, syntax, examples for better understanding. You can also go through our other related articles to learn more –

  1. Inline Function in C
  2. Recursive Function in C
  3. Hashing Function in C
  4. Recursive Function in C++

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More