GTU OOP Program - 8
8). Write a program that reads an integer and displays all its smallest factors in increasing order. For example, if input number is 120, the output should be as follows:2,2,2,3,5.
import java.util.Scanner; class Program_08 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int num = input.nextInt(); int index = 2; // Num to test as factors // finding all the smallest factors while (num / index != 1) { // Test as a factor of num if (num % index == 0) { System.out.print(index + ", "); num /= index; } else { index++; } } System.out.println(num + "."); } }
Output
Comments