Java Program to Subtract Two Numbers

You are currently viewing Java Program to Subtract Two Numbers

Hello reader! If you are looking to subtract two numbers using java program then refer to this article. Yes, I’m going to teach the concept of the Java Program to Subtract Two Numbers here. Let’s move on to this article.

Java Program to Subtract Two Numbers

Now, We can apply the two input values directly into this program to subtract two numbers in java.

//Java program for substraction two numbers
public class Substract_Two_Num1{
public static void main(String args[]){
//variable declaration and initialization
int result,num1=50,num2=24;
    
result=num1-num2;//calculate subtraction using equation
System.out.println("Substraction of integers: "+result);
//displays result of subtraction
}
}

Explanation

As usual, We have to declare the three variables like “num1”, “num2”, and the “result”. Later, We need to provide the two input values.

The input values will be subtracted using the condition (result=num1-num2) and the output will be stored in the “result” variable. Finally, The system.out.printIn() function will help to print the result on the output screen.

Output

The output of the java program to subtract two numbers will be given below for your reference.

Subtraction of integers: 26
Read Also : Java Program to Multiply Two Numbers

Note

We have already published the articles like Subtract Two Numbers in JavaScript and Subtract Two Numbers in PHP on our webportal. If you want to know then please read those articles.

Java Program to Subtract Two Numbers

We can get the values from a user to add two numbers using java program here.

//Java program for substraction two numbers
import java.util.Scanner;
public class Substract_Two_Num{
public static void main(String args[]){
//variable declaration 
int result,num1,num2;
 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the first number  ");
num1=scan.nextInt();//read the first input 
System.out.println("Enter the second number  ");
num2=scan.nextInt();//read the second input
result=num1-num2;//calculate subtraction
System.out.println("Substraction of entered integer: "+result);
//display the result
}
}

Execution

Initially, We need to import the java.util.scanner class into this program. Secondly, We have to declare the respective variables to store both input and output values.

After executing this program, The program asks you to provide the input values using the new scanner() function. Later, The output will calculate using the condition (result=num1-num2).

Finally, The system.out.printIn() function will show the result on the output screen.

Output

I’ll show the example output of the java program to subtract two numbers below for your reference.

Enter the first number  30
Enter the second number  20
Substraction of entered integer: 10

Final Words

I hope this article helps you to Subtract Two Numbers using Java. If you face any issues please let me know via the comment section. Share this article with other Java developers via social networks. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply