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

Tutoriais Básicos de Java

Java Controle de Fluxo

Java Array

Java Orientação a Objetos (I)

Java Orientação a Objetos (II)

Java Orientação a Objetos (III)

Tratamento de Exceção Java

Java Lista (List)

Java Fila (Queue)

Java Mapa (Map)

Java Conjunto (Set)

Java Entrada e Saída (I/O)

Java Reader/Writer

Java Other Topics

Java program to find the factors of a number

Java Examples Comprehensive

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

Example: Factors of a positive integer

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

When running the 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 with the for loop until i <= number is false. In each iteration, it will check if the number is completely divisible by i (i is the condition of the factor of the number), and the value of i will increase1.

Java Examples Comprehensive