Question:
Write a program that takes a number as input and prints all its factors except 1 and the number itself.. If the number has only two factors (1 and the number itself), then the program should print -1.
First what is factor: What Is a Factor in Math? A factor of a number is a number that divides the given number evenly or exactly, leaving no remainder. Note that when studying factors of a number, we only consider positive integers. A factor cannot be a fraction or a decimal.
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int f = scan.nextInt();
for (int i =1; i<=f; i++) {
if (f % i ==0){
System.out.print(i);
}
}
}
}
Comments
Post a Comment