Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

fabs() in C++

  • Difficulty Level : Expert
  • Last Updated : 09 Apr, 2018

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

Syntax:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
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




    // 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;
    }

    OUTPUT :

    fabs of -10 is 10
    

    # CODE 2




    // 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;
    }

    OUTPUT :

    fabs of -35 is 35
    



    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :

    Start Your Coding Journey Now!