Prime Checker

  • + 0 comments

    **Why do they add 4th and 5th together instead of individually? **

    Shouldn't the result of such a task include 5 lines of solution instead of 4?

    ob.checkPrime(n1); ob.checkPrime(n1,n2); ob.checkPrime(n1,n2,n3); ob.checkPrime(n1,n2,n3,n4,n5);

    **Why they did it like that? **

    For me better will be sth like this: import java.util.*;

    public class Solution {

    public static void main(String[] args) {
       Prime prime = new Prime();
        prime.checkPrime();
    
    
    }
    

    }

    class Prime{

    public static void checkPrime(){
        Scanner scanner = new Scanner(System.in);
        int number;
        int divisors =2;
        ArrayList<String> primeList = new ArrayList<>();
    
        for(int i=0;i<5;i++){
            number=scanner.nextInt();
            for(int j=2;j<=Math.sqrt(number);j++){
                if(number%j==0){
                    divisors++;
                }
            }
            if(divisors==2&&number!=1){
                primeList.add(String.valueOf(number));
            }
            divisors=2;
            System.out.println(String.join(" ",primeList));
        }
    
    }
    

    }