Printing prime number from 1 to 100

前端 未结 4 2218
南方客
南方客 2021-02-11 10:15

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

4条回答
  •  悲哀的现实
    2021-02-11 10:44

        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;
    }
    

提交回复
热议问题