Table of Contents
In this tutorial, we will discuss the Java program to subtraction of two numbers
In this post, we are going to learn how to find subtraction of two numbers via different 5 ways
Program 1
public class SubtractTwoNum{ public static void main(String args[]){ int num1=35,num2=56,sub; //declare and initialize variables num1,num2,sub; sub=num2-num1;//find subtract of two numbers System.out.println("Subtract of two numbers: "+sub); //display the subtraction of two numners } }
When the above code is executed, it produces the following result
Subtract of two numbers:21
In this program,
Program 2
import java.util.Scanner; public class SubtractTwoNum1{ public static void main(String args[]){ int sub;//declare variable to find subtraction Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); sub=num2-num1;//find subtract of two numbers System.out.print("subtraction of "+num2+"-"+num1+" :"+sub); //display the subtraction of two numners } }
When the above code is executed, it produces the following result
Enter the value to num1: 125 Enter the value to num2: 230 subtraction of 230-125 :105
In the program,
Program 3
import java.util.Scanner; public class SubtractTwoNum2{ public static void main(String args[]){ int sub;//declare variable to find subtraction Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); System.out.print("subtraction of "+num2+"-"+num1+" :"+subtraction(num1,num2)); //display the subtraction of two numners } static int subtraction(int x,int y){ return y-x; } }
When the above code is executed, it produces the following result
Case 1
Enter the value to num1: 123 Enter the value to num2: 234 subtraction of 230-125 :111
Case 2
Enter the value to num1: 234 Enter the value to num2: 123 subtraction of 230-125 :-111
In the program,
Program 4
import java.util.Scanner; public class SubtractTwoNum3{ public static void main(String args[]){ int sub;//declare variable to find subtraction Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); System.out.print("subtraction of "+num2+"-"+num1+" :"+subtraction(num1,num2)); //display the subtraction of two numners } static int subtraction(int num1,int num2){ if(num2==0) return num1; else return subtraction((num1-1),(num2-1)); } }
When the above code is executed, it produces the following result
Case 1
Enter the value to num1: 789 Enter the value to num2: 456 subtraction of 230-125 :333
Case 2
Enter the value to num1: 345 Enter the value to num2: 567 subtraction of 230-125 :-222
In the program,
Program 5
import java.util.Scanner; public class SubtractTwoNum4{ public static void main(String args[]){ int sub;//declare variable to find subtraction Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); int n1=num1; int n2=num2; System.out.print("subtraction of "+n1+"-"+n2+" :"+subtract(num1,num2)); //display the subtraction of two numners } static int subtract(int num1,int num2) { if(num2==0) return num1; return subtract(num1^num2,(~num1 & num2)<<1); } }
When the above code is executed, it produces the following result
Case 1
Enter the value to num1: 678 Enter the value to num2: 789 subtraction of 230-125 :-111
Case 2
Enter the value to num1: 765 Enter the value to num2: 543 subtraction of 230-125 :222
Suggested post
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.