abs function in cpp code example

Example 1: how to find absolute value in c++

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

int main()
{
	int x = -5;
	long y = -2371041;

	int a = abs(x);
	long b = abs(y);

	cout << "abs(" << x << ") = |" << x << "| = " << a << endl;
	cout << "abs(" << y << ") = |" << y << "| = " << b << endl;
}

/* output
abs(-5) = |-5| = 5
abs(-2371041) = |-2371041| = 2371041*/

Example 2: abs c++

#include <stdlib.h>     /* abs */

int main ()
{
  int n,m;
  n=abs(23); // n=23
  m=abs(-11); // m=11
  return 0;
}

Example 3: abs in c++

[Mathematics] |x| = abs(x) [C++ Programming]

Example 4: abs in cpp

int a =4;
int b =5;

int ans = abs(a-b); //ALWAYS RETURN POSITIVE NUMBER.

Tags:

Cpp Example