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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago