This Java program prompts the user to enter an integer number, and then converts the number to its absolute value using a ternary operator. The result is printed to the console.
Note that the ternary operator is used to write a concise conditional expression that evaluates to one of two values based on a condition. In this case, the ternary operator replaces the use of an if statement to determine the sign of the input number.
import java.util.*; public class Integer_Absolute { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an Integer Number : "); int n = input.nextInt(); int abs_val = (n >= 0) ? n : -n; System.out.println("Given value : "+n); System.out.println("Convert Integer to Absolute value : "+abs_val); } }
Enter an Integer Number : -2653 Given value : -2653 Convert Integer to Absolute value : 2653
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions