The project of this assignment is to how to do c comparisons
Solution
Please find the required solution:
import java.util.Scanner;
public class FactorGenerator {
//Implementation of the find factors
public static String findFactors(int n) {
StringBuilder builder = new StringBuilder();
// iterate until the number is even
while (n % 2 == 0) {
builder.append(\"2\").append(\" \");
n = n / 2;
}
// n is odd here
for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
// while the odd number is divided
while (n % i == 0) {
builder.append(i).append(\" \");
n = n / i;
}
}
// handle the prime numbers
if (n > 2)
builder.append(n).append(\" \");
return builder.toString();
}
// Test class input and invoke method find factors
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print(\"Enter the number greater than 2:\");
int input = keyboard.nextInt();
System.out.println(findFactors(input));
keyboard.close();
}
}
Sample output:
Test-1:
Enter the number greater than 2:315
3 3 5 7
Test-2:
Enter the number greater than 2:999
3 3 3 37

