In this article, we will discuss the concept of the Write a program to Subtract two numbers in Java
In this post, we are going to learn how to write a program to find the subtraction of two numbers in Java programming language
The program use to find subtraction of given two integer numbers
Program 1
//Java program for substraction two numbers public class Substract_Two_Num1{ public static void main(String args[]){ //variable declaration and initialization int result,num1=45,num2=78; result=num2-num1;//calculate subtraction using equation System.out.println("Substraction of integers: "+result); //displays result of subtraction } }
When the above code is executed, it produces the following result
Subtraction of integers: 33
This program allows the user to enter two integer numbers Then the program find the results of subtraction the given two number
Program 2
//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=num2-num1;//calculate subtraction System.out.println("Substraction of entered integer: "+result); //display the result } }
When the above code is executed, it produces the following result
Enter the first number:234 Enter the second number:345 Subtraction of entered integer:111
The program use to find subtraction of given two integer numbers
Program 3
//Java program for substraction two numbers public class Substract_Two_Num2{ public static void main(String args[]){ //variable declaration and initialization float result,num1=24.6f,num2=78.9f; result=num2-num1;//calculate subtraction System.out.format("Substraction of entered floating point numbers:%.2f ",result); //displays result of subtraction } }
When the above code is executed, it produces the following result
Subtraction of entered floating-point numbers:54.30
This program allows the user to enter two float numbers Then the program find the results of subtraction the given two number
Program 4
//Java program for substraction two numbers import java.util.Scanner; public class Substract_Two_Num3{ public static void main(String args[]){ //variable declaration float result,num1,num2; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the first number "); num1=scan.nextFloat();//read the first input System.out.println("Enter the second number "); num2=scan.nextFloat();//read the second input result=num2-num1; System.out.format("Substraction of entered floats:%.2f ",result); //display the result } }
When the above code is executed, it produces the following result
Enter the first number: 345.66 Enter the second number: 543.56 Subtraction of entered floats:197.90
Suggested for you
Operator in Java language
Data type and variable in Java language
Class and main method in Java language
Similar post
Find sum of two numbers in Java
Find sum of two numbers in Java using recursion
Find sum of two numbers in Java without Arithmetic operator
Find sum of natural numbers in Java language
Find sum of natural numbers in Java language using recursion
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…