w3resource

Java: Divide the two specified integers using subtraction operator


Divide Using Subtraction

Write a Java program to divide the two given integers using the subtraction operator.

Visual Presentation:

Java Basic Exercises: Divide the two specified integers using subtraction operator.


Sample Solution:

Java Code:

 import java.util.Scanner;

public class Solution {
    // Method to perform division using subtraction
    public static float divide_using_subtraction(int dividend, int divider) {
        if (divider == 0) {
            return 0; // If the divider is zero, return 0 (division by zero error)
        }
        
        float result = 0; // Initialize the result variable to store the quotient
        
        // Perform division using subtraction
        while (dividend > divider) {
            dividend -= divider; // Subtract the divider from the dividend
            result++; // Increment the result (quotient)
        }
        
        float decimalPart = (float) dividend / (float) divider; // Calculate the decimal part of the quotient
        result += decimalPart; // Add the decimal part to the result
        return result; // Return the final result (quotient)
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in); // Create Scanner object to take user input
        System.out.print("Input the dividend: "); // Prompt user to input the dividend
        int dividend = in.nextInt(); // Read input dividend
        
        System.out.print("Input the divider: "); // Prompt user to input the divider
        int divider = in.nextInt(); // Read input divider
        
        System.out.println("\nResult: " + divide_using_subtraction(dividend, divider)); // Display the result of division
    }
}

Sample Output:

Input the dividend:  625
Input the divider:  25

Result: 25.0

Flowchart:

Flowchart: Java exercises: Divide the two specified integers using subtraction operator.



For more Practice: Solve these Related Problems:

  • Write a Java program to multiply two integers using only repeated addition.
  • Write a Java program to compute the modulus of two integers using the subtraction operator.
  • Write a Java program to perform integer division through repeated subtraction, accounting for negative inputs.
  • Write a Java program to implement division without using division or multiplication operators.

Go to:


PREV : Binary Zeros Count.
NEXT : Move Positives Right.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.