Advertisement
Spocoman

03. Sum Prime Non Prime

Aug 30th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumPrimeNonPrime {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int primeSum = 0, nonPrimeSum = 0, number;
  7.  
  8.         String input;
  9.         while (!(input = scanner.nextLine()).equals("stop")) {
  10.             number = Integer.parseInt(input);
  11.             if (number >= 0) {
  12.                 if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0) {
  13.                     if (number == 2 || number == 3 || number == 5 || number == 7) {
  14.                         primeSum += number;
  15.                     } else {
  16.                         nonPrimeSum += number;
  17.                     }
  18.                 } else {
  19.                     primeSum += number;
  20.                 }
  21.             } else {
  22.                 System.out.println("Number is negative.");
  23.             }
  24.         }
  25.  
  26.         System.out.printf("Sum of all prime numbers is: %d\n", primeSum);
  27.         System.out.printf("Sum of all non prime numbers is: %d\n", nonPrimeSum);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement