Printing prime number from 1 to 100

前端 未结 7 1652
情书的邮戳
情书的邮戳 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条回答
  •  你的背包
    2021-02-11 10:38

    public static ArrayList prime(int limit){

        ArrayList primes = new ArrayList<>();
        for(int p = 2; p <= limit; p++) {
            int count = 0;
            for(int i=2; i < p; i++) {
    
                if (p%i == 0) {
                    count++;
                }
            }
    
            if (count == 0) {
                primes.add(p);
            }   
        }
        return primes;
    }
    

提交回复
热议问题