C++ abs() Absolute Value with Examples

In C++, abs() function returns the absolute value of the given argument. By using abs function you can return absolute value for an int, float, double, long etc. data types in C++.

Basic Syntax

Following is the basic syntax for abs() function in C++:

int abs(int number)
double abs(double number)
float abs(float number)

And the parameters are:

Parameter Description
number int, float, double, short, long or byte number.

Return Value

abs() function returns absolute value of the given number.

  • For an integer value, it will return an integer
  • For float value, it will return a float and so on.


public class Demo { 

   public static void main(String args[]) {
      int num1 = -8;
      float num2 = -90.0f;
      double num3 = -100.01;
      long num4 = 20345676;
      short num5 = 98;
      byte num6 = -5;

      System.out.println("Integer num1 = "+Math.abs(num1));
      System.out.println("Float num2 = "+Math.abs(num2));
      System.out.println("Double num3 = "+Math.abs(num3));
      System.out.println("Long num4 = "+Math.abs(num4));
      System.out.println("Short num5 = "+Math.abs(num5));
      System.out.println("Byte num6 = "+Math.abs(num6));

The output for the above program is as given below:

Integer num1 = 8
Float num2 = 90.0
Double num3 = 100.01
Long num4 = 20345676
Short num5 = 98
Byte num6 = 5