The program accepts two integers and prints their absolute difference as the output in Java


The java.lang.Math.abs(int a) returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

Show

Declaration

Following is the declaration for java.lang.Math.abs() method

public static int abs(int a)

Parameters

a − the argument whose absolute value is to be determined

Return Value

This method returns the absolute value of the argument.

Exception

NA

Example

The following example shows the usage of lang.Math.abs() method.

package com.tutorialspoint; import java.lang.*; public class MathDemo { public static void main(String[] args) { // get some integers to find their absolute values int x = 175; int y = -184; // get and print their absolute values System.out.println("Math.abs(" + x + ")=" + Math.abs(x)); System.out.println("Math.abs(" + y + ")=" + Math.abs(y)); System.out.println("Math.abs(-0)=" + Math.abs(-0)); } }

Let us compile and run the above program, this will produce the following result −

Math.abs(175)=175 Math.abs(-184)=184 Math.abs(-0)=0

java_lang_math.htm

Last update on May 28 2022 09:41:08 (UTC/GMT +8 hours)

Write a Java program that accepts two integers and then prints the sum, the difference, the product, the average, the distance (the difference between integer), the maximum (the larger of the two integers), the minimum (smaller of the two integers).

Test Data Input 1st integer: 25

Input 2nd integer: 5

The program accepts two integers and prints their absolute difference as the output in Java

Sample Solution:

Java Code:

import java.util.Scanner; public class Exercise9 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input 1st integer: "); int firstInt = in.nextInt(); System.out.print("Input 2nd integer: "); int secondInt = in.nextInt(); System.out.printf("Sum of two integers: %d%n", firstInt + secondInt); System.out.printf("Difference of two integers: %d%n", firstInt - secondInt); System.out.printf("Product of two integers: %d%n", firstInt * secondInt); System.out.printf("Average of two integers: %.2f%n", (double) (firstInt + secondInt) / 2); System.out.printf("Distance of two integers: %d%n", Math.abs(firstInt - secondInt)); System.out.printf("Max integer: %d%n", Math.max(firstInt, secondInt)); System.out.printf("Min integer: %d%n", Math.min(firstInt, secondInt)); } }

Sample Output:

Input 1st integer: 25 Input 2nd integer: 5 Sum of two integers: 30 Difference of two integers: 20 Product of two integers: 125 Average of two integers: 15.00 Distance of two integers: 20 Max integer: 25 Min integer: 5

Flowchart:

The program accepts two integers and prints their absolute difference as the output in Java

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program that reads a number and display the square, cube, and fourth power.
Next: Write a Java program to break an integer into a sequence of individual digits.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Convert boolean to int in Java

int myInt = myBoolean ? 1 : 0;

Note : true = 1 and false = 0

Ref: https://bit.ly/3dC3vto

Given two integers N and X. The task is to print the absolute difference between the first X and last X digits in N. Considering the number of digits is atleast 2*x.

Examples: 



Input: N = 21546, X = 2 Output: 25 The first two digit in 21546 is 21. The last two digit in 21546 is 46. The absolute difference of 21 and 46 is 25. Input: N = 351684617, X = 3 Output: 266

Simple Appr

:  
  • Store the last x digits of the number in last.
  • Find the number of digits in the number.
  • Remove all the digits except the first x.
  • Store the first x integers of the number in first.

Below is the implementation of the above approach:  

long long digitsCount(long long n)

long long absoluteFirstLast(long long n, int x)

    long long len = digitsCount(n);

    return abs(first - last);

    long long n = 21546, x = 2;

    cout << absoluteFirstLast(n, x);

static int digitsCount(int n)

static int absoluteFirstLast(int n, int x)

    int len = digitsCount(n);

    return Math.abs(first - last);

public static void main(String args[])

    System.out.println(absoluteFirstLast(n, x));

def absoluteFirstLast(n, x) :

    return abs(first - last);

if __name__ == "__main__" :

    print(absoluteFirstLast(n, x));

static int digitsCount(int n)

static int absoluteFirstLast(int n, int x)

    int len = digitsCount(n);

    return Math.Abs(first - last);

public static void Main(String []args)

    Console.Write(absoluteFirstLast(n, x));

function absoluteFirstLast($n, $x)

    return abs($first - $last);

echo absoluteFirstLast($n, $x);

function absoluteFirstLast(n, x)

    let len = digitsCount(n);

    return Math.abs(first - last);

    document.write(absoluteFirstLast(n, x));

Time Complexity: O(X+k) where X is the given integer and k is the number of digits in n.
Auxiliary Space: O(1), since no extra space has been taken.


Article Tags :

Practice Tags :