Java program to display factors of a number

https‮/:‬/www.theitroad.com

Here's a Java program to display the factors of a number:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int number = scanner.nextInt();
        scanner.close();

        System.out.print("Factors of " + number + ": ");

        for (int i = 1; i <= number; i++) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
}

In this program, we first prompt the user to enter a positive integer using the Scanner class.

We then use a for loop to iterate over the numbers from 1 to number. For each iteration, we check if the current number is a factor of number by checking if number % i == 0. If it is, we print the current number as a factor of number.

The output of this program will be:

Enter a positive integer: 24
Factors of 24: 1 2 3 4 6 8 12 24

You can replace 24 with any other positive integer to display its factors.