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

5 different ways to Divide two numbers in Java using scanner

Posted on November 24, 2025

Table of Contents

  • 5 Different ways to Divide two numbers in Java using scanner
    • Different Ways to Divide of two numbers in Java
      • Using the Division “/” operator
      • Divide two numbers using while loop
      • Divide two numbers using method
      • Divide two numbers Using recursion
      • Divide two numbers Using for loop(Repeated Subtraction)
    • Related

5 Different ways to Divide two numbers in Java using scanner

In this article, we will discuss the concept of Java Code Examples – 5 Different ways to Divide two numbers in Java using scanner

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

5 different ways to Divide two numbers in Java using scanner
Divide two numbers

 

Different Ways to Divide of two numbers in Java

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

Using the Division “/” operator

Divide two numbers using divisional (/) operator with user input. The simplest and most common way to divide two numbers

Program 1

// Division of two numbers in Java
// Basic Division using / operator
import java.util.Scanner;
class Basic_Division{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in); 
        System.out.print("Input first number: "); 
        int num_1=sc.nextInt(); 
    System.out.print("Input second number: "); 
        int num_2=sc.nextInt();
        System.out.println("Division of two numbers: "+(num_1/num_2));
        
    }
}

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

Input first number: 100
Input second number: 5
Division of two numbers: 20

 

Divide two numbers using while loop

Divide two numbers using divisional (/) operator

Program 2

// Divide two numbers using while loop with user input
import java.util.Scanner;
class DivideNumbers {
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);
        // Taking user input
        System.out.print("Enter the dividend: ");
        int dividend = input.nextInt();

        System.out.print("Enter the divisor: ");
        int divisor = input.nextInt();
        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

Enter the dividend: 45
Enter the divisor: 9
Quotient using loop: 5
Remainder: 0

Divide two numbers using method

Divide two numbers using method with user input

Program 3

// Divide two numbers using method with user input
import java.util.Scanner;
class DivideNum2 {
// Method to divide two numbers
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) {
Scanner sc = new Scanner(System.in);
System.out.println("Division using user difined method ");

// Taking user input
System.out.print("Enter the dividend: ");
double dividend = sc.nextDouble();

System.out.print("Enter the divisor: ");
double divisor = sc.nextDouble();

// Calling the divide method
double result = divide(dividend, divisor);

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

}
}


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

Division using user defined method
Enter the dividend: 56
Enter the divisor: 7
Division result: 8.0
Division is: 8.0

 

Program 4

Divide two numbers Using recursion

Divide two numbers Using recursion with user input

// Divide two numbers using recursion with user input
import java.util.Scanner;
class divideRecursionInput{
    //Recursive method to perform integer division
    public static int divide(int dividend, int divisor){
        if(dividend<divisor) return 0;
        return 1+divide (dividend-divisor,divisor);
    }
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        System.out.println("Enter dividend: ");
        int dividend=input.nextInt();
            System.out.println("Enter divisor: ");
        int divisor=input.nextInt();
        System.out.println("Quotient:" +divide(dividend,divisor));
        
    }
}

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

Enter dividend:
35
Enter divisor:
7
Quotient:5

 

Program 5

Divide two numbers Using for loop(Repeated Subtraction)

Divide two numbers Using for loop(Repeated Subtraction)

// Divide two numbers using for loop with user input
import java.util.Scanner;
class divideForInput{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        System.out.println("Enter dividend: ");
        int dividend=input.nextInt();
            System.out.println("Enter divisor: ");
        int divisor=input.nextInt();
        int quotient=0;
        for(; dividend>=divisor; dividend-=divisor){
            quotient++;
        }
        System.out.println("Quotient:" +quotient);
        System.out.println("Remainder:" +dividend);
        
    }
}

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

Enter dividend:
60
Enter divisor:
8
Quotient:7
Remainder:4

 

above programs Allow users to enter numbers at runtime.

 

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