Java Program to Subtract two Numbers

This article shows you how to write a Java program to subtract two numbers. Here, you will see multiple solutions for it such as subtraction of two static numbers, the subtraction of two dynamic given numbers, and Subtracting two Numbers using command-line arguments.

Subtract two Numbers in java

The subtraction of two numbers in java is very simple. Let’s assume we have two integer numbers x=50 and y=10. Now we will take a subtract integer variable to calculate the subtraction of x and y. Let’s see the code for it below:

public class SubtractTwoNumbers {

	public static void main(String[] args) {
		
		// declare and initialize two integer numbers
		int x=50;
		int y=10;

		// declare another variable to hold the substracted value
		int subtract;

		// calculate the substraction 
		subtract = x - y;

		// print the calculated subtract value
		System.out.println("The substraction of two Numbers: " + subtract);

	}

}

OUTPUT:

The substraction of two Numbers: 40

Subtract two dynamically given Numbers in Java

Here we will take two integer numbers from the user dynamically (at run-time). To take input at run-time, we can take the Scanner class object for reading the user’s inputs. The Scanner class is used to get user input, and it is found in the “java.util” package. Let’s see the complete code for it below:

import java.util.Scanner;

public class SubtractTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, subtract;

		// create scanner object to take dynamic input
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter first number: ");
		// taking first number
		x = sc.nextInt();

		System.out.print("Enter second number: ");
		// taking second number
		y = sc.nextInt();

		// calculate the subtraction
		subtract = x - y;

		// print the calculated sum
		System.out.println("The subtraction of the given two Numbers: " + subtract );

	}

}

OUTPUT:

Enter first number: 100
Enter second number: 40
The substraction of the given two Numbers: 60

Subtract two Numbers in Java using command-line Arguments

Now we will see, the subtraction of two numbers that are given from command line arguments. Generally, the inputs that come from the command line arguments are in String datatype. They are stored as strings in the String array passed to the args[] parameter of the main() method in Java. So we will convert the string to an integer using the parseInt() method before storing it in integer datatype. The command line arguments should be passed to the main() method at run-time.

Let’s see the code for it below:

public class SubtractTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, subtract;
        
        // read first number
		x = Integer.parseInt(args[0]);

        // read second number
		y = Integer.parseInt(args[1]);

		// calculate the subtraction
		subtract = x - y;

		// print the calculated subtract value
		System.out.println("The subtraction of given two Numbers: " + subtract);

	}

}

OUTPUT:

The subtraction of given two Numbers: 35

In order to execute the above program, we will open the command prompt and compile it first with the command >javac SubtractTwoNumbers.java then run the command >java SubtractTwoNumbers 55 20

That’s it! You may continue reading Java Program to Multiply two Numbers.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *