ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

abs() Function with Example in C++ (cstdlib/stdlib.h)

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

C++ abs() function

abs() function is a library function of cstdlib header. It is used to get the absolute value. It accepts a value (int, long int, long long it) and returns its absolute value. This method is an overloaded method of abs() method of cmath header (that is used for getting absolute value of the floating-point values).

Syntax of abs() function:

C++11:

    int abs (int n);
    long int abs (long int n);
    long long int abs (long long int n);

Parameter(s):

  • n – represents an Integral value whose absolute value to found.

Return value:

It returns the absolute value of n.

Example:

    Input:
    n = -10
    
    Function call:
    abs(n);
    
    Output:
    10

    Input:
    n = 10
    
    Function call:
    abs(n);
    
    Output:
    10

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

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

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

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

    n = -10;
    cout << "abs(" << n << "): " << abs(n) << endl;

    n = 10;
    cout << "abs(" << n << "): " << abs(n) << endl;

    n = -12345678;
    cout << "abs(" << n << "): " << abs(n) << endl;

    n = 12345678;
    cout << "abs(" << n << "): " << abs(n) << endl;

    return 0;
}

Output

abs(-10): 10
abs(10): 10 
abs(-12345678): 12345678
abs(12345678): 12345678

Reference: C++ abs() function

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.