Home » C++ programming language

fabs() function with example in C++

C++ fabs() function: Here, we are going to learn about the fabs() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on April 26, 2019

C++ fabs() function

fabs() function is a library function of cmath header, it is used to find the absolute value of the given number, it accepts a number and returns absolute value.

Note: abs() function of cmath header can also be used for the same purpose.

Syntax of fabs() function:

    fabs(x);

Parameter(s): x – is the number whose absolute value is returned.

Return value: double – it returns double value that is the absolute value of x.

Example:

    Input:
    float x = -1012.232;

    Function call:
    fabs(x);

    Output:
    1012.232

C++ code to demonstrate the example of fabs() function

// C++ code to demonstrate the example of 
// fabs() function
#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    float result;
    
    x = -10;
    cout<<"fabs("<<x<<"): "<<fabs(x)<<endl;

    x = -1012.232;
    cout<<"fabs("<<x<<"): "<<fabs(x)<<endl;    

    x = 1012.232;
    cout<<"fabs("<<x<<"): "<<fabs(x)<<endl;    

    x = -.908;
    cout<<"fabs("<<x<<"): "<<fabs(x)<<endl;    
    
    return 0;
}

Output

fabs(-10): 10
fabs(-1012.23): 1012.23
fabs(1012.23): 1012.23
fabs(-0.908): 0.908






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.