Prime Checker

Sort by

recency

|

355 Discussions

|

  • + 0 comments

    solution for java 8 import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Prime ob = new Prime();
    
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int d = scan.nextInt();
        int e = scan.nextInt();
    
        ob.checkPrime(a);
        ob.checkPrime(a,b);
        ob.checkPrime(a,b,c);
        ob.checkPrime(a,b,c,d,e);
    
        scan.close();
    }
    

    }

    class Prime{
        void checkPrime(int... numbers){
            for(int num: numbers){
                if(isPrime(num)){
                    System.out.print(num + " ");
                }
            }
            System.out.println();
        }
        private boolean isPrime(int n){
            if(n < 2) return false;
            for(int i = 2; i <= Math.sqrt(n); i++){
                if(n % i == 0) return false;
            }
            return true;
        }
    }
    
  • + 0 comments

    you can add

    import static java.lang.System.in;
    in java 7 to fix the compilation error

    and enjoy

  • + 0 comments
    class Prime{
        public void checkPrime(int... input){
            boolean isPrime;
            for(int i: input){
                isPrime = true;
                for(int j=2; j*j<=i; j++){
                    if(i%j == 0){
                        isPrime=false;
                        break;
                    }
                }
                if(isPrime && i!=1){
                    System.out.print(i + " ");
                }
            }
            System.out.println();
        }
    }
    
  • + 0 comments

    Here is Prime checker in java solution - https://programmingoneonone.com/hackerrank-prime-checker-problem-solution-in-java.html

  • + 1 comment

    The problem description does not fit the code. For me the solution class is empty, there is no locked code.