Calculations

Write a program to Subtract two numbers in Java

Write a program to Subtract two numbers in Java

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

Subtract two numbers

Code to find the subtraction of  two numbers 

Subtract two integer number

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

 

Subtract two integer number using user input

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

Subtract two floating-point number

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

 

Subtract two floating-point number using user input

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

 

C Program for calculating factorial of a number using recursion
Subtract two numbers in C++ programming
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 week ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

4 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

4 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago