Java Prime Checker HackerRank Solution

Java Prime Checker - HackerRank Solution

Hello Friends, How are you? Today I will solve the HackerRank Java Prime Checker Problem with a straightforward explanation. In this article, you will get more than one approach to solve this problem. So let's start-

HackerRank Java Prime Checker - Problem Statement

You are given a class Solution and its main method in the editor. Your task is to create a class Prime. The class Prime should contain a single method checkPrime.

The locked code in the editor will call the checkPrime method with one or more integer arguments. You should write the checkPrime method in such a way that the code prints only the prime numbers.

Note: You may get a compile time error in this problem due to the statement below:

This was added intentionally, and you have to figure out a way to get rid of the error.

Input Format

There are only five lines of input, each containing one integer.

Output Format

There will be only four lines of output. Each line contains only prime numbers depending upon the parameters passed to checkPrime in the main method of the class Solution. In case there is no prime number, then a blank line should be printed.

Sample Input

2 1 3 4 5 {codeBox}

Sample Output

2 2 2 3 2 3 5 {codeBox}

Java Prime Checker HackerRank Solution

Java Prime Checker - Hacker Rank Solution

Approach I: Java Prime Checker - Solution HackerRank

``````// ========================
//       Information
// ========================

// Name: Java Prime Checker HackerRank Problem
// Difficulty: Medium
// Max Score: 25
// Language: Java 7

// ========================
//         Solution Start
// ========================

// Java Prime Checker - Hacker Rank Solution Start

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.reflect.*;

// code start here
import static java.lang.System.*;

class Prime {
public void checkPrime(int... values) {
for (int i = 0; i < values.length; i++) {
if (PrimeChecker.isPrime(values[i])) {
System.out.print(values[i] + " ");
}
}
System.out.print("\n");
}
}

class PrimeChecker {
public static boolean isPrime(int n) {
if (n <= 1) { return false; }
for (int i = 2; i <= (int)(n/2); i++) {
if (n % i == 0) { return false; }
}

return true;
}
}
// Code ends here

public class Solution {
public static void main(String[] args) {
try{
Prime ob=new Prime();
ob.checkPrime(n1);
ob.checkPrime(n1,n2);
ob.checkPrime(n1,n2,n3);
ob.checkPrime(n1,n2,n3,n4,n5);
Method[] methods=Prime.class.getDeclaredMethods();
Set<String> set=new HashSet<>();
for(int i=0;i<methods.length;i++)
{
if(set.contains(methods[i].getName()))
{
break;
}

}
{
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

// Java Prime Checker Hacker Rank Solution END
// MyEduWaves``````

Approach II: Java Prime Checker - Solution HackerRank

``````// ========================
//       Information
// ========================

// Name: Java Prime Checker HackerRank Problem
// Difficulty: Medium
// Max Score: 25
// Language: Java 7

// ========================
//         Solution Start
// ========================

// Java Prime Checker - Hacker Rank Solution Start

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.reflect.*;

// Code Start Here
import static java.lang.System.in;
class Prime{
boolean chk(int n){
if(n<2)return false;
for(int i=2;i*i<=n;i++)if(n%i==0)return false;
return true;
}
void checkPrime(Integer... a){
boolean f=false;
for(int i=0;i<a.length;i++){
if(chk(a[i])){
if(f)java.lang.System.out.print(" ");
f=true;
java.lang.System.out.print(a[i]);
}
}
java.lang.System.out.println();
}
}

// Code ends here

class Solution{
public static void main(String[] args){
try{
Prime ob=new Prime();
ob.checkPrime(n1);
ob.checkPrime(n1,n2);
ob.checkPrime(n1,n2,n3);
ob.checkPrime(n1,n2,n3,n4,n5);
Method[] methods=Prime.class.getDeclaredMethods();
Set<String> set=new HashSet<>();
for(int i=0;i<methods.length;i++){
if(set.contains(methods[i].getName())){
break;
}
}
}
}catch(Exception e){
System.out.println(e);
}
}
}

// Java Prime Checker Hacker Rank Solution END
// MyEduWaves``````

Disclaimer: The above Problem ( Java Prime Checker ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java Prime Checker - Hackerrank Problem and try to solve it again.

All the Best!