Home »
C++ programming language
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
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.
Sample Input and Output
Input:
n = -10
Function call:
abs(n);
Output:
10
Input:
n = 10
Function call:
abs(n);
Output:
10
Example
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