C language programming exercise: Use the pow() function to achieve the value of x to the power of y

The C language pow() function is used to find the value of x to the power of y.

Header file: math.h

Grammar/prototype:

double pow(double x,double y);

Parameter Description:

x: Double precision number. y: double-precision number.

Return value: the value of x raised to the power of y.

[Example 1] Use the pow() function to find the 6th power of 4, the code is as follows:

#include <stdio.h>

#include <math.h>

int main () {

  double x = 4, y = 6; //Assign initial value to the variable

  double result = pow(x, y); //Find a to the b power

  printf("%lf\n", result);

  return 0;

}

operation result:

4096.000000

[Example 2] Use the C language pow() function to calculate the power of the value entered by the user.

#include <stdio.h>

#include <math.h>

int main () {

  double a, b; //Define two variables

  printf("Please enter a number:"); //output prompt information

  scanf("%lf", &a); //Get the value entered by the user

  printf("Please enter the power:"); //output prompt information

  scanf("%lf", &b); //Get the value entered by the user

  printf("result: %lf\n", pow(a, b)); //output result

  return 0;

}

operation result:

Please enter a number: 10

Please enter the power: 4

result: 10000.000000

Circumstances that may cause the error:

If the base x is negative and the exponent y is not an integer, it will cause a domain error. If the base x and exponent y are both 0, it may or may not cause a domain error; this is related to the implementation of the library. 

If the base x is 0 and the exponent y is negative, it may or may not cause a domain error or pole error; this is related to the implementation of the library. If the return value ret is too large or too small, it will cause a range error.

error code:

If a domain error error occurs, the global variable errno will be set to EDOM; if a pole error or range error error occurs, the global variable errno will be set to ERANGE.

In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/114980622