Printing prime number from 1 to 100

前端 未结 7 1551
情书的邮戳
情书的邮戳 2021-02-11 10:12

This program is supposed to output the prime numbers between 1 and 100. Will anyone please explain me the flow the programme below? I am having difficulty in writing the progra

7条回答
  •  -上瘾入骨i
    2021-02-11 10:46

        public static List getPrimeNumbers(int from , int to) {
        List list = new ArrayList<>();
        for (int i = from;i <= to; i++) {
            int count = 0;
            for (int num = i; num>=1;num--) {
                if(i%num == 0){
                    count++;
                }
            }
            if(count ==2) {
                list.add(i);
            }
        }
        return list;
    }
    

提交回复
热议问题