Advertisement
marking2112

NLEqualSumPrimeNonPrime

Oct 23rd, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 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.  
  7.         String input = scanner.nextLine();
  8.         int sumPrime = 0;
  9.         int sumNormal = 0;
  10.  
  11.         while (!input.equals("stop")) {
  12.             int num = Integer.parseInt(input);
  13.             if (num < 0) {
  14.                 System.out.println("Number is negative.");
  15.             }else {
  16.                 int i = num;
  17.                 int counter = 0;
  18.  
  19.                 for(;num >= 1; num--) {
  20.  
  21.                     if ( i % num == 0 )
  22.                     {
  23.                         counter++;
  24.                     }
  25.                 }
  26.                 if (counter == 2)
  27.                 {
  28.                     sumPrime += i;
  29.                 }
  30.                 else
  31.                 {
  32.                     sumNormal += i;
  33.                 }
  34.             }
  35.             input = scanner.nextLine();
  36.         }
  37.         System.out.printf("Sum of all prime numbers is: %d%n", sumPrime);
  38.         System.out.printf("Sum of all non prime numbers is: %d%n", sumNormal);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement