Question

My assignment is to check if a number is a prime number, or if it , then display what it divides by ( for example 12 divides by 1, 2, 3, 4, 6, and 12.)

import java.util.Scanner;
public class kt_4_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number:");
        num = scanner.nextInt();
        int array[] = new int[num];
        for (int count; count <= num; count++) {
            if (num % count == 0) {
                // need to store the numbers somehow, possibly in an array.
            }
            // Or, if the amount of numbers it can be divided by
                    //is 2 ( meaning 1 and itself ), then say the number is a prime.
        }
    }
}
Was it helpful?

Solution

The simplest way of storing an undefined amount of numbers is with an ArrayList. An ArrayList is just a dynamic list that grows and shrinks to meet the need of the programmer.

ArrayList<Integer> factors = new ArrayList<>();

Java Documentation ArrayList

OTHER TIPS

You can use lists. Or you can print them in the if condition

What you need here is a data structure of variable size, which can be extended. I would suggest, you use Vector class defined in java.util package.

http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top