Library Function fabs in Math Class in C

Library Function fabs of math.h Header File

In this section, we will discuss about library function fabs in math class which is used in C.The C header file math.h contains the standard math library functions that can be used for performing various mathematical operations. The fabs function returns the absolute value of the number.

library function fabs in math class

Library Function fabs of math.h Header File

  • In C programming language the fabs function is included in math.h header file.
  • The range of input argument which is passed to fabs function is not limited here as it can be any value from int to float or double but then has to be type casted into double type.

Declaration of fabs function

double fabs(double arg) 

Parameters of fabs function

The fabs function accepts a single input argument  which is of double type.

Parameter        Description
double value        This value of double function ranges to all positive and negative numbers.

Return value of fabs function

The fabs function returns the absolute value of the number and it’s type is double.

ParameterReturn Value
Absolute ValueIt returns the value in double.

Implementation of Library Function math.h fabs()

Example 1:

The following code shows the use of fabs function.

Run
#include<stdio.h> 
#include<conio.h> 
int main()
{
    double x, result;
    x = -1.5;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    x = 11.3;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    x = 0;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);
    return 0;
}

Output:

|-1.50| =  1.50
|11.30| =  11.30
|0.00| =  0.00

Example 2:

Run
#include<stdio.h>
#include<conio.h> 
int main ()
{
  double a = 980;
  double b = -1231;
  double res;
  res = fabs (a);
  printf ("The absolute value of %.3lf is %.3lf\n", a, res);
  res = fabs (b);
  printf ("The absolute value of %.3lf is %.3lf\n", b, res);
  return 0;
}

Output:

The absolute value of 980.000 is 980.000
The absolute value of -1231.000 is 1231.000

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription