labs() Function with Example in C++

C++ labs() function: Here, we are going to learn about the labs() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 28, 2020

C++ labs() function

labs() function is a library function of cstdlib header. It used to get the absolute of the given value. This function is similar to the abs() function except for the type of the parameter, it is used for the long integer values. It accepts a parameter and returns the absolute value.

Syntax of labs() function:

C++11:

    long int labs (long int n);

Parameter(s):

  • n – represents the value whose absolute value to found.

Return value:

The return type of this function is long int, it returns the absolute value.

Example:

    Input:
    n = -1234567890123
    
    Function call:
    labs(n);
    
    Output:
    1234567890123

    Input:
    n = 1234567890123
    
    Function call:
    labs(n);
    
    Output:
    1234567890123

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

// C++ code to demonstrate the example of
// labs() function

#include <iostream>
#include <cstdlib>
using namespace std;

// main() section
int main()
{
    long int n;

    n = -1234567890123;
    cout << "labs(" << n << "): " << labs(n) << endl;

    n = 1234567890123;
    cout << "labs(" << n << "): " << labs(n) << endl;

    n = -1111222233334444;
    cout << "labs(" << n << "): " << labs(n) << endl;

    n = 1111222233334444;
    cout << "labs(" << n << "): " << labs(n) << endl;

    return 0;
}

Output

labs(-1234567890123): 1234567890123
labs(1234567890123): 1234567890123
labs(-1111222233334444): 1111222233334444
labs(1111222233334444): 1111222233334444

Reference: C++ labs() function




Comments and Discussions!

Load comments ↻






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