English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Base Tutorial

Contrôle de flux Java

Java Tableau

Java Orienté Objet (I)

Java Orienté Objet (II)

Java Orienté Objet (III)

Java Exception Handling

Java Liste (List)

Java Queue (file d'attente)

Java Map

Java Set

Java Entrée/Sortie (I/O)

Lecteur Java/Écrivain

Java other topics

Java program to find the factors of a number

Java complete list of examples

In this program, you will learn how to use the for loop in Java to display all factors of a given number.

Example: factors of positive integers

public class Factors {
    public static void main(String[] args) {
        int number = 60;
        System.out.print("" + number + " is the factor of: ");
        for (int i = 1; i <= number; ++i) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
}

When running this program, the output is:

60's factors are: 1 2 3 4 5 6 10 12 15 20 30 60

In the above program, the number to be found is stored in the variable number (6in (0) .

Iterate the for loop until i <= number is false. In each iteration, it will check whether the number can be completely divided by i (i is the condition that the number is a factor), and the value of i will increase1.

Java complete list of examples