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. DeclarationFollowing is the declaration for java.lang.Math.abs() method public static int abs(int a)Parametersa − the argument whose absolute value is to be determined Return ValueThis method returns the absolute value of the argument. ExceptionNA ExampleThe 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)=0java_lang_math.htm 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 Sample Solution: Java Code: Sample Output: Flowchart: 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.
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 :
Below is the implementation of the above approach:
Time Complexity: O(X+k) where X is the given integer and k is the number of digits in n. Article Tags : Practice Tags : |