We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Prime {
public 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 <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
}
public class Solution {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n1 = Integer.parseInt(br.readLine());
int n2 = Integer.parseInt(br.readLine());
int n3 = Integer.parseInt(br.readLine());
int n4 = Integer.parseInt(br.readLine());
int n5 = Integer.parseInt(br.readLine());
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<>();
boolean overload = false;
for (Method method : methods) {
if (set.contains(method.getName())) {
overload = true;
break;
}
set.add(method.getName());
}
if (overload) {
throw new Exception("Overloading not allowed");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Prime Checker
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.; import java.lang.reflect.;
class Prime { public void checkPrime(int... numbers) { for (int num : numbers) { if (isPrime(num)) { System.out.print(num + " "); } } System.out.println(); }
}
public class Solution { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n1 = Integer.parseInt(br.readLine()); int n2 = Integer.parseInt(br.readLine()); int n3 = Integer.parseInt(br.readLine()); int n4 = Integer.parseInt(br.readLine()); int n5 = Integer.parseInt(br.readLine());
}