write a program in java to find factors of a number entered by user
Answers
import java.util.Scanner;
public class Factor{
public static void main(Stirng..args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println("The factor of :"+a+" is");
for(int i=1;i<=a;i++){
if(a%i==0){
System.out.println(i+" ");
}
}
}
}
Hope it helps ^_^
I have also added all the possible outputs
import java.util.Scanner;
public class FactorTeller{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int i, num;
System.out.print("Enter a number in order to know its factors: ");
num = sc.nextInt();
System.out.println("The factors of the given number are as follows: ");
for (i = 1; i <= num; i++){
if(num % i == 0){
System.out.println(i);
}
}
}
}
Outputs:
Enter a number in order to know its factors: 20
The factors of the given number are as follows:
1
2
4
5
10
20