How to subtract in java – Java Program to Subtract Two Numbers

How to subtract in java: In the previous article, we have seen Java Program to Find Square and Square root of a Number

In this article we are going to see how to subtract two numbers using Java Programming.

Java Program to Subtract Two Numbers

Subtraction is an arithmetic operation which is represented by symbol ‘-‘. Generally we removes objects from a collection in case of subtraction.

For example:

15 - 5 = 10
1000 - 700 = 300
65 - 30 = 35

Let’s see different ways to subtract two numbers.

Method-1: Java Program to Subtract Two Numbers By Using Subtraction ‘-‘ Operator

Approach:

  1. Create scanner class object.
  2. Take user input for two numbers.
  3. Find the result using ‘-‘ operator.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        //Scanner class object created
        Scanner input = new Scanner(System.in);
        //taking user input of first number
        System.out.print("Enter the first number: ");
        int num1 = input.nextInt();
        //taking user input of second number
        System.out.print("Enter the second number: ");
        int num2 = input.nextInt();
        //finding subtraction value by using arithmetic subtract operator
        //and printing the result
        System.out.println("The difference of " + num1 + " and " + num2 + " is " + (num1 - num2));
    }
}
Output:

Enter the first number: 60
Enter the second number: 20
The difference of 60 and 20 is 40

Method-2: Java Program to Subtract Two Numbers By Using Recursion

Approach:

  1. Create scanner class object.
  2. Take user input for two numbers.
  3. Use the recursive function.
  4. Call the function again and again by reducing each number by one until num2 becomes one.
  5. If num2 becomes 0 return num1.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //Scanner class object created 
        Scanner input = new Scanner(System.in); 
        //taking user input of first number 
        System.out.print("Enter the first number: "); 
        int num1 = input.nextInt(); 
        //taking user input of second number 
        System.out.print("Enter the second number: "); 
        int num2 = input.nextInt();
        System.out.println("The difference of " + num1 + " and " + num2 + " is " + subtraction(num1, num2));
    }

    //user defined method subtraction()
    static int subtraction(int num1, int num2) 
    {
        if (num2 == 0)
            return num1;
        else
            //calling subtraction() method recursively
            return subtraction((num1 - 1), (num2 - 1));
    }
}

Output:

Enter the first number: 10
Enter the second number: 7
The difference of 10 and 7 is 3

Method-2: Java Program to Subtract Two Numbers By Using Bitwise Operators (Without Arithmetic Operators)

Approach:

  1. Create scanner class object.
  2. Take user input for two numbers.
  3. Use the recursive function.
  4. Call the function recursively by passing num1 xor num2 as 1st parameter and num1’ and num2 left shift by one bit.
  5. If num2 becomes 0 return num1.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner input = new Scanner(System.in);
        //Taking input of two numbers from user
        System.out.print("Enter the first number: ");
        int num1 = input.nextInt();
        System.out.print("Enter the second number: ");
        int num2 = input.nextInt();
        //calling subtract_bitwise() method and printing result
        System.out.println("The difference of " + num1 + " and " + num2 + " is " + subtract_bitwise(num1, num2));
    }

    //subtract_bitwise() method to perform subtraction
    static int subtract_bitwise(int num1, int num2) 
    {
        if (num2 == 0)
            return num1;
        //recursively calling subtract_bitwise()
        return subtract_bitwise(num1 ^ num2, (~num1 & num2) << 1);

    }
}

Output:

Enter the first number: 12
Enter the second number: 8
The difference of 12 and 8 is 4

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: