Find the absolute value of a number entered through the keyboard


The program begins by creating a Scanner object to read input from the user. It then prompts the user to enter a number using the nextInt() method of the Scanner class.

Next, the program checks if the entered number is negative by using an if statement. If the number is negative, it is multiplied by -1 to make it positive, which is the absolute value of the number.

Finally, the program prints the absolute value of the number using the println() method of the System.out object.

Source Code

import java.util.Scanner;
class Absolute
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Number :");
		int num = input.nextInt();
		if(num<0)
			num = (-1)*num;
		System.out.print("Absolute Number is : "+num);
	}
}

Output

Enter The Number :-456
Absolute Number is : 456

Example Programs