Java Tutorial
🔍
Java ProgramsBasics & I/ODivide Two Numbers

Divide Two Numbers in Java

beginner·  Basics & I/O  ·  Arithmetic

Problem

Dividing two int values in Java performs integer division, discarding any remainder.

Given two integers, divide the first by the second and print the result.

Input
20, 4
Output
Quotient: 5

Java Program

Java
public class DivideTwoNumbers { public static void main(String[] args) { int a = 20; int b = 4; int quotient = a / b; // integer division — no fractional part System.out.println("Quotient: " + quotient); } }

Output

Quotient: 5

Core Logic

The / operator between two int values performs integer division, keeping only the whole-number part of the result.

How It Works
  1. 1a holds 20 and b holds 4.
  2. 2a / b evaluates to 5, since 20 divides evenly by 4.
  3. 3The result is stored in quotient and printed with a label.
With a = 20 and b = 4, a / b evaluates to 5, so the program prints "Quotient: 5".
💡

Key Point: int division truncates any remainder — 7 / 2 gives 3, not 3.5. Use double values (or a cast) when a fractional result is needed.

Key Concepts

/ operatorinteger divisionint

Related Programs