Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

How to Divide two numbers in Java| 5 different ways

Posted on October 28, 2025

Table of Contents

  • How to Divide two numbers in Java| 5 different ways
    • Division of two numbers in Java
      • Using simple division operator
      • Using Scanner class
      • Using User defined method
      • Using loops (Repeated Subtraction method)
      • Using recursion
    • Related

How to Divide two numbers in Java| 5 different ways

In this article, we will discuss the concept of How to Divide two numbers in Java| 5 different ways

In this post, we are going to learn how to write a program to 5 Ways to Division of Two Numbers (With Examples) in  Java programming language.

How to Divide two numbers in Java| 5 different ways
Divide two numbers

Division of two numbers in Java

here, I’ll give you separate short programs for each method so it’s easier to understand

Using simple division operator

The simplest and most common way to divide two numbers

Program 1

// Divide two nunbers using simple division operator

class DivideNum {
    public static void main(String[] args) {
        int num1=24;
        int num2=4;
        int result=num1/num2;
         System.out.println("Division using / operator ");
        System.out.println("Division is: "+result);
    }
}

When the above code is executed, it produces the following result

Division using / operator
Division is: 6

 

Using Scanner class

Division of two numbers Using Allows users to enter numbers at runtime.

Program 2

// Divide two nunbers using Scanner Class
import java.util.Scanner;
class DivideNum2 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter first number ");
        double num1=input.nextDouble();
         System.out.println("Enter second number ");
        double num2=input.nextDouble();
        double result=num1/num2;
         System.out.println("Division using Scanner ");
        System.out.println("Division is: "+result);
    }
}

When the above code is executed, it produces the following result

Enter first number
33
Enter second number
11
Division using Scanner
Division is: 3.0

 

Using User defined method

Division is done inside a method for reusability.

Program 3

// Divide two nunbers using user defined-method
class DivideNum2 {
    public static double divide(double a, double b){
        if(b==0){
            System.out.println("Cannot divide by zero ");
            return 0;
        }
        return a/b;
        
    }
    public static void main(String[] args) {
        double result=divide(25,5);
         System.out.println("Division using user difined method ");
         System.out.println("Division is: "+result);
        
    }
}

When the above code is executed, it produces the following result

Division using user defined method
Division is: 5.0

 

Using loops (Repeated Subtraction method)

performs division without using the / operator – good for understanding logic

Program 4

// Divide two numbers using while loop

class DivideNumbers {
    public static void main(String[] args) {
        int dividend=30;
        int divisor=4;
        int quotient=0;
        while(dividend>= divisor){
         dividend-=divisor;
         quotient++;
        }
        System.out.println("Quotient using loop: "+quotient);
        System.out.println("Remainder: "+dividend);
    }
}

When the above code is executed, it produces the following result

Quotient using loop: 7
Remainder: 2

 

Using recursion

Program 5

// Divide two numbers using recursion
class DivideNum2 {
     // Method to divide two numbers
    public static int divide(int dividend, int divisor){
        if(divisor==0){
            
            throw new ArithmeticException("Cannot divide by zero ");
        }
        if(dividend<divisor){
        return 0;
        }
        return 1+divide(dividend - divisor,divisor);
    }
    public static void main(String[] args) {
        
         System.out.println("Division using user difined method ");

        // Calling the divide method
        double result = divide(20,4);

        // Displaying the result
        System.out.println("Division result: " + result);
        
    }
}

When the above code is executed, it produces the following result

Division using user defined method
Division result: 5.0

 

In the above program, the program defines a recursive function add(a,b).

  • if b=0; it simply returns a
  • Otherwise, it calls itself with (a+1) (b-1) until b become 0

The recursion gradually increases a and decreases b until the total sum is reached.

 

Similar post

Java program to division of two numbers – 5 ways

PHP program to division of two numbers

C# program to divide two numbers

C++ program to divide two numbers – 6 ways

JavaScript program to divide two numbers – 4 ways

 

Java code two divide two numbers using method

Java program to division of two integer numbers

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes