SHARE
TWEET

Untitled

a guest Apr 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class SumPrimeNonPrime {
  4.     public static void main(String[] args) {
  5.  
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         String input = scanner.nextLine();
  9.         int sumPrime = 0;
  10.         int sumNonPrime = 0;
  11.  
  12.         while (!input.equalsIgnoreCase("stop")) {
  13.  
  14.             int number = Integer.parseInt(input);
  15.             boolean isPrime = true;
  16.  
  17.             if (number < 0) {
  18.                 System.out.println("Number is negative.");
  19.                 input = scanner.nextLine();
  20.                 continue;
  21.             }
  22.             if (number == 0 || number == 1) {
  23.                 isPrime = false;
  24.             }
  25.             for (int i = 2; i < number; i++) {
  26.                 if (number % i == 0 && i != number) {
  27.                     isPrime = false;
  28.                 }
  29.  
  30.             }
  31.             if (isPrime) {
  32.                 sumPrime += number;
  33.             } else {
  34.                 sumNonPrime += number;
  35.             }
  36.             input = scanner.nextLine();
  37.         }
  38.  
  39.         System.out.printf("Sum of all prime numbers is: %d", sumPrime).println();
  40.         System.out.printf("Sum of all non prime numbers is: %d", sumNonPrime).println();
  41.     }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top