Finding factors of a given integer

前端 未结 14 1374
终归单人心
终归单人心 2020-12-16 19:52

I have something like this down:

int f = 120;
for(int ff = 1; ff <= f; ff++){
    while (f % ff != 0){            
}

Is there anything w

14条回答
  •  不知归路
    2020-12-16 20:16

    Here is how to get all factors of the given number.

    public class Factors {
    
        public static void main(String[] args){
            int n = 420;
    
            for(int i=2; i<=n; i++){
                while(n%i==0){
                    System.out.println(i + "| " + n);
                    System.out.println(" -----");
                    n = n/i;
                }
            }
        }
    }
    

    Output:

    2| 420
     -----
    2| 210
     -----
    3| 105
     -----
    5| 35
     -----
    7| 7
     -----
    

提交回复
热议问题