C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » C programs » math.h header file functions

Function fabs() to get the absolute value of a number in C






In this article, we are going to learn about the fabs() function of math.h header file and try to understand it with the help of an example.
Submitted by Manu Jemini, on March 30, 2018

In mathematics, certain situations demand the value to be absolute. This is very common and easy for human to get the absolute value. But in programming it will be a little tricky and anyway we will make a function in the end.

But there is a better way to do the same thing and then the math.h comes handy. This library has an inbuilt function which takes a double and returns the absolute value.

So to get it function running, just call this function with a library and store the result in another variable.

For Example: The absolute value of -21.0 is 21.0

Explanation:

    a = -3.0 
    check,
        if its negative, if true, multiple it by -1 
        else return the same number

This is a function from math.h file, therefore it needs that file to be included in the program.




math.h - fabs() function Example in C

#include <stdio.h>
#include <math.h>

int main()
{
	// Defining variables
	float a,b;

	// Assigning value for getting fabs value
	a = 30.33;

	// Calculating the fabs of the value a
	b = fabs(a);

	// Displaying the result for the user
	printf("The calculated absolute value is: %f \n\n", b);
	
	return 0;
}

Output

math.h - fabs()  in c language







You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions