fabs() in C++

The fabs() function returns the absolute value of the argument.
Mathematically |a|. If a is value given in the argument.

Syntax:

double fabs(double a);
float fabs(float a);
int fabs(int a);

Parameter:



  • The fabs() function takes a single argument, a whose absolute value has to be returned.
  • Return:

  • The fabs() function returns the absolute value of the argument.
  • Error:

  • It is mandatory to give both the arguments otherwise it will give error
    no matching function for call to ‘fabs()’ like this.
  • # CODE 1

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // fabs() function
    #include <cmath>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        int a = -10, answer;
      
        answer = fabs(a);
        cout << "fabs of " << a 
             << " is " << answer << endl;
      
        return 0;
    }

    chevron_right

    
    

    OUTPUT :

    fabs of -10 is 10
    

    # CODE 2

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // fabs() function
    #include <cmath>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        long int a = -35;
        double answer;
      
        answer = fabs(a);
        cout << "fabs of " << a << " is " 
             << answer << endl;
      
        return 0;
    }

    chevron_right

    
    

    OUTPUT :

    fabs of -35 is 35
    


    My Personal Notes arrow_drop_up

    pawanasipugmailcom

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.