Advertisement
Guest User

Programming Basic - Sum Prime Non Prime [Java]

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