In the previous article, we have seen Java Program to Find the Last Digit of a Number
In this article we will see how to convert a negative number to a positive number by using Java programming language.
Java Program to Convert a Negative Number to a Positive Number
If a number is less than 0 then we say it as a negative number.
For example:
-67 -987 -4 Above numbers are negative numbers.
Let’s see different ways to convert a negative number to a positive number.
Method-1: Java Program to Convert a Negative Number to a Positive Number By Using Math.abs() Method
Approach:
- Declare an integer variable say ‘
num‘ and take the value as user input. - Check if the number is less than 0 then it is negative number then convert it to positive by using
Math.abs()and print the positive number. - Else the number is already a positive number then print the number.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
//integer variable 'num' which will hold the original number
int num;
//integer variable positiveNum which will hold the positive number
int positiveNum;
//asking the user to input a number
System.out.println("Enter a number: ");
num = sc.nextInt();
//if number is less than 0
if (num < 0)
{
//converting to positve number by using Math.abs()
positiveNum = Math.abs(num);
System.out.println("The negative number after converting it to positive number: " + positiveNum);
}
else
{
System.out.println("The number is already a positive number: " + num);
}
}
}
Output: Enter a number: -9 The negative number after converting it to positive number: 9
Method-2: Java Program to Convert a Negative Number to a Positive Number By Multiplying -1
Approach:
- Declare an integer variable say ‘
num‘ and take the value as user input. - Check if the number is less than 0 then it is negative number then multiply it with -1 and print the positive number.
- Else the number is already a positive number then print the number.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
//integer variable 'num' which will hold the original number
int num;
//integer variable positiveNum which will hold the positive number
int positiveNum;
//asking the user to input a number
System.out.println("Enter a number: ");
num = sc.nextInt();
//if number is less than 0
if (num < 0)
{
//Multiplying 'num' with -1
positiveNum = num*(-1);
System.out.println("The negative number after converting it to positive number: " + positiveNum);
}
else
{
System.out.println("The number is already a positive number: " + num);
}
}
}
Output: Enter a number: -789 The negative number after converting it to positive number: 789
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: