"Print Numbers Divisible by 5 in Java"

Laura Jun 11, 2026

Understanding the Basics of Printing Numbers Divisible by 5 in Java

Printing numbers divisible by 5 is a fundamental concept in Java programming that requires a clear and concise approach. In this article, we will explore the process of printing numbers that meet this specific condition, highlighting the various methods and techniques involved. Whether you're a beginner or an experienced developer, understanding how to print numbers divisible by 5 in Java is an essential skill to master.

Requirements and Pre-requisites

Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.
Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.

Before diving into the code, it's crucial to understand the basics of Java programming, including data types and control structures. Familiarity with printing and looping concepts is also necessary to grasp the following examples.

Method 1: Using a For Loop

To print numbers divisible by 5, we can utilize a for loop to iterate through a range of numbers and check for divisibility.

numbers between 1 to 100 which are divisible by 3, 5 in Java
numbers between 1 to 100 which are divisible by 3, 5 in Java

  • Checking Divisibility with Modulus
    The modulus operator (\%) is used to find the remainder of a division operation. By checking if the remainder is 0, we can determine if a number is divisible by 5.
  • Example Code
    java // Initialize a loop from 1 to 20 for (int i = 1; i <= 20; i++) { // Check if i is divisible by 5 if (i % 5 == 0) { System.out.println(i); } }

This code will print numbers 5, 10, 15, and 20.

Method 2: Using a For-Each Loop

We can also use a for-each loop to print numbers divisible by 5.

80+ Pattern Programs In Java
80+ Pattern Programs In Java

  • Creating an Array
    First, we need to create an array containing numbers from 1 to 20.
  • Using the For-Each Loop
    java int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // Iterate through the array and print numbers divisible by 5 for (int num : numbers) { if (num % 5 == 0) { System.out.println(num); } }

This code achieves the same result as the previous example.

Using An Array and Conditional Statement

In this method, we can use an array to store numbers divisible by 5 and then print them using a conditional statement.

  • Initializing an Array
    We start by declaring an array to hold numbers divisible by 5. The array size will be determined by the upper limit of the iteration.
  • Populating the Array and Printing
    java int size = 20; // Upper limit of iteration int[] divisibleBy5 = new int[size]; int index = 0; // Iterate from 1 to 20 and populate the array with numbers divisible by 5 for (int i = 1; i <= 20; i++) { if (i % 5 == 0) { divisibleBy5[index] = i; index++; } } // Print the array for (int i : divisibleBy5) { System.out.println(i); }

This approach yields the same result as the previous examples.

Common Mistakes and Best Practices

  • Avoid Hardcoding Values
    Hardcoding values, especially numbers, can make the code rigid and difficult to maintain.
  • Use Meaningful Variable Names
    Variable naming is a crucial aspect of writing readable and maintainable code.

Frequently Asked Questions (FAQs)

Q: What is the condition for checking if a number is divisible by 5?
A: The number passed the divisibility test if its remainder when divided by 5 is 0.

Q: Can we use loops other than for loop to print numbers divisible by 5?
A: Yes, we can use for-each loops or while loops to achieve the same result.

Q: How do I write an efficient loop to print numbers from 1 to 100 that are divisible by 5?
A: Use a for loop to iterate through the range from 1 to 100 and use the modulus operator to check for divisibility by 5.

Conclusion and Next Steps

Printing numbers divisible by 5 in Java is a simple yet essential skill for any developer to master. This article covered various methods to achieve this using loops, array initialization, and conditional statements. By understanding the code examples provided, you should be able to tackle more complex scenarios and apply the principles to your own coding projects. Practice and experiment with different approaches to improve your understanding and solidify your skills. Whether you're a beginner or an experienced developer, the ability to print numbers divisible by 5 in Java is a fundamental skill that will serve you well in your programming journey. As you continue to develop your Java skills, keep in mind the importance of effective coding practices and the benefits of experimenting with different approaches. This is an essential step towards becoming a proficient Java developer.

C Program To Check Number Is Divisible By 11 Or Not Using (VEDIC MATH)
C Program To Check Number Is Divisible By 11 Or Not Using (VEDIC MATH)
Inverted Pyramid number pattern in Java - Codeforcoding
Inverted Pyramid number pattern in Java - Codeforcoding
Java Random numbers
Java Random numbers
58 Number Pattern Programs In Java | Pyramid and Diamond Pattern Programs
58 Number Pattern Programs In Java | Pyramid and Diamond Pattern Programs
How to Reverse Numbers in Java, C, C#?
How to Reverse Numbers in Java, C, C#?
Deep Dive into java numbers-  Methods with syntax and examples
Deep Dive into java numbers- Methods with syntax and examples
the number system is shown in red and blue
the number system is shown in red and blue
Number patterns in Java
Number patterns in Java
How To Create Pyramid Of Numbers In Java?
How To Create Pyramid Of Numbers In Java?
an image of a computer screen with many lines on the screen and text below it
an image of a computer screen with many lines on the screen and text below it
間違い探しプリント 3桁数字 初級問題
間違い探しプリント 3桁数字 初級問題
How to print in Java?
How to print in Java?
an image of a computer screen with the words and numbers on it, including two arrows
an image of a computer screen with the words and numbers on it, including two arrows
What is number class in Java?
What is number class in Java?
Java Data Types Explained
Java Data Types Explained
Java Programming Fundamentals Notes
Java Programming Fundamentals Notes
Insertion Sort Algorithm in Java with Example
Insertion Sort Algorithm in Java with Example
Number Series Tricks & Patterns | Complete Reasoning Notes for Beginners
Number Series Tricks & Patterns | Complete Reasoning Notes for Beginners
two different types of numbers that appear to be written in the form of an array
two different types of numbers that appear to be written in the form of an array
a poster showing different types of numbers and symbols for children's learning materials, including books
a poster showing different types of numbers and symbols for children's learning materials, including books
30 Days of Java – Day 5 | Java Switch Statement Explained for Beginners
30 Days of Java – Day 5 | Java Switch Statement Explained for Beginners
1D Array in Java Explained | Java Array Basics for Beginners
1D Array in Java Explained | Java Array Basics for Beginners
two images showing the same size and number of each item
two images showing the same size and number of each item