// Print the prime factors of a number N // // Divide by 2 until there are no more 2's // Divide by 3 until there are no more 3's // Divide by 4 until there are no more 4's // Divide by 5 until there are no more 5's // ... until we reach 1 // e.g. // 300/2 -> 150/2 -> 75/3 -> 25/5 -> 5/5 -> 1 // Program as developed in class - see handout // for completed solution import java.util.*; public class PrimeFactors { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter a number: "); int num = console.nextInt(); int div = 2; boolean firstTime = true; while (num != 1) { // System.out.println(div); if (num%div == 0) { if (firstTime) { System.out.print(div); firstTime = false; } else { System.out.print(" * " + div); } num = num / div; } else { div++; } } System.out.println(); } }