Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Arrays{
- private static String getInput(String prompt){
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- System.out.print(prompt);
- System.out.flush();
- try{
- return stdin.readLine();
- }catch(Exception e){
- return "Error: " + e.getMessage();
- }
- }
- public static void main(String args[]) {
- String input = getInput("Enter the number of elements: ");
- int n = Integer.parseInt(input);
- int [] inputArray = new int [n];
- int [] isPrime = new int [n];
- int [] isNonPrime = new int [n];
- int i, countisPrime = 0, countisNonPrime = 0;
- for(i = 0; i<n; i++){
- input = getInput("Enter an element: ");
- inputArray[i] = Integer.parseInt(input);
- int divisor = 2, result = 0;
- if(inputArray[i] < 2){
- isNonPrime[countisNonPrime++] = inputArray[i];
- continue;
- }
- while (divisor <= inputArray[i]/2){
- if(inputArray[i]%divisor == 0){
- result = 1;
- break;
- }
- else divisor++;
- }
- if(result != 1)
- isPrime[countisPrime++] = inputArray[i];
- else
- isNonPrime[countisNonPrime++] = inputArray[i];
- }
- int smallisPrime = isPrime[0];
- System.out.println("\n\n"+countisPrime+" Prime found:-");
- for(i = 0; i<countisPrime; i++){
- System.out.println(isPrime[i]);
- if(isPrime[i] < smallisPrime)
- smallisPrime = isPrime[i];
- }
- System.out.println(smallisPrime+" is the smallest Prime.");
- int largeisNonPrime = isNonPrime[0];
- System.out.println("\n\n"+countisNonPrime+" Non-Prime found:-");
- for(i = 0; i<countisNonPrime; i++){
- System.out.println(isNonPrime[i]);
- if(isNonPrime[i] > largeisNonPrime)
- largeisNonPrime = isNonPrime[i];
- }
- System.out.println(largeisNonPrime+" is the largest Non-Prime.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement