Home  >  Article  >  Backend Development  >  How to calculate the absolute value in C language

How to calculate the absolute value in C language

coldplay.xixi
coldplay.xixiOriginal
2021-01-08 16:46:1789548browse

How to implement absolute value in C language: 1. If it is an integer number, you can use the abs function to get its absolute value; 2. If it is a floating-point number, you can use the fabs function to get its absolute value.

How to calculate the absolute value in C language

The operating environment of this article: Windows 7 system, Dev-C version 5.11, Dell G3 computer.

How to implement absolute value in C language:

1. In C language, you can use the abs or fabs function to get the absolute value of a number, which is contained in math.h header file, so add the header file to the code first, as shown in the figure below.

How to calculate the absolute value in C language

#2. If it is an integer number, you can use the abs function to get its absolute value. The code is as shown in the figure below.

How to calculate the absolute value in C language

#3. If it is a floating-point number, you can use the fabs function to get its absolute value. The code is as shown in the figure below.

How to calculate the absolute value in C language

#4. Finally, you can use the Printf function to print out the numbers converted into absolute values ​​and view the code running results, as shown in the figure below.

How to calculate the absolute value in C language

#5. After debugging and running the code, you can see the result after converting the number into an absolute value, as shown in the figure below.

How to calculate the absolute value in C language

The above is the detailed content of How to calculate the absolute value in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]