Java Program

Java program to subtraction of two numbers | 5 different Methods

Java program to subtraction of two numbers | 5 different Methods

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

 

Subtraction of two numbers

Subtract of two numbers – Standard method

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,

  • integer variables num1,num2 are declared and initialized
  • The num1 and num2 both are subtracted together and the value is asigned to the variable sub.
  • Then,the program is displayed the output of subtraction using System.out.println();

 

Subtract of two numbers – Entered by user

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,

  • Declare variables num1, num2, sub
  • The program asks input from the user
  • Then the user enters the input values for num1, num2.
  • The program will read the input using scanner class and store the variables num1 and num2 respectively
  • The num1 and num2 both are subtracted together and the value assigned to the sub variable
  • Then, the program displays the value of the sub using System.out.println() function

 

 

Subtract of two numbers – Using user defined method

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,

  • Declare variables num1, num2, sub
  • The program asks input from the user
  • Then the user enters the input values for num1, num2.
  • The program will read the input using scanner class and store the variables num1 and num2 respectively
  • Define the method(subtraction()) for find subtraction
  • Call the method to produced output
  • finally, the program displays the value of subtraction using System.out.println() function

 

Subtract of two numbers – Using recursion

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,

  • Declare variables num1, num2 and sub
  • The program asks input from the user
  • Then the user enters the input values for num1, num2.
  • The program will read the input using scanner class and store the variables num1 and num2 respectively
  • Define the method(subtraction()) for find subtraction recursively
  • Call the method to produced output
  • finally, the program displays the value of subtraction using System.out.println() function

Subtract of two numbers – Without Arithmetic operator

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

Method in Java language

Operator in Java language

 

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.