Advertisement
yovkovbpfps

Nested LOOPS SUM PRIME NON PRIME

Apr 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PrimeNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String input = scanner.nextLine();
  10. int sumPrime = 0;
  11. int sumNonprime = 0;
  12.  
  13. while (!"stop".equals(input)) {
  14.  
  15. int num = Integer.parseInt(input);
  16. int count = 0;
  17.  
  18. for (int i = 2; i <= num/2; i++) {
  19.  
  20. if (num % i == 0) {
  21. count++;
  22. break;
  23. }
  24.  
  25. }
  26.  
  27. if ( num < 0 ) {
  28. System.out.println("Number is negative.");
  29. } else if ( count > 0 || num == 1) {
  30. sumNonprime+=num;
  31. } else {
  32. sumPrime+=num;
  33. }
  34.  
  35. input = scanner.nextLine();
  36. /* System.out.println(count);*/
  37.  
  38. }
  39.  
  40. System.out.printf("Sum of all prime numbers is: %d\n", sumPrime);
  41. System.out.printf("Sum of all non prime numbers is: %d", sumNonprime);
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement