Wednesday, July 19, 2017

Factor Program

/*A program that displays the factors of a number.
Created by Eyasu Mulugeta A.K.A BlackDiamond0014 on 6/23/2017.
eyasu@india.com
*/
import java.util.ArrayList;
import java.util.Scanner;
public class FactorProgram {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num=0, size;
        System.out.print("Enter a number to find its factors : ");
        num = in.nextInt();
        ArrayList<Integer> factors = new ArrayList<Integer>();
        System.out.println();
        System.out.print("The factors of " + num + " are ");
        for (int i = 1, j = 0; i <= num; i++, j++) {
            if (num % i != 0)
                continue;
            factors.add(i);
        }
        for (int k:factors) {
            System.out.print(k);
            if(k==num){
                break;
            }
            System.out.print( ", ");
        }
    }
}
Share: