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!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.