Question:-
Betsy teaches her daughter to find the factors of a given number. When she provides a number to her daughter, she should tell the factors of that number. Help her to do this, by writing a program.
Write a class FindFactor.java and write the main method in it.
Note :
- If the input provided is negative, ignore the sign and provide the output. If the input is zero
- If the input is zero the output should be “No Factors”.
Sample Input 1 :
54
Sample Output 1 :
1, 2, 3, 6, 9, 18, 27, 54
Sample Input 2 :
-1869
Sample Output 2 :
1, 3, 7, 21, 89, 267, 623, 1869
Code:-
import java.util.*; import java.lang.Math; class FindFactor{ public static void main(String[] args){ int n,x; //StringJoiner joiner = new StringJoiner(", "); Scanner sc = new Scanner(System.in); x=sc.nextInt(); n=Math.abs(x); if(n==0){ System.out.println("No Factors"); } else if(n<0 || n>0){ for(int i=1;i<=n;i++){ if(n%i==0){ //joiner.add(String.valueOf(i)); //String out = joiner.toString(); System.out.print(i); if(n==i) System.out.print(""); else System.out.print(", "); } } } } }