Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.SoftUni;
- import java.util.Scanner;
- public class Problem03 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String n = scan.nextLine();
- boolean flag = false;
- boolean negative = false;
- int primeSum = 0;
- int nonPrimeSum = 0;
- while (!n.equals("stop")) {
- int currentN = Integer.parseInt(n);
- if (currentN < 0) {
- n = scan.nextLine();
- negative = true;
- continue;
- }
- for (int i = 2; i <= currentN / 2; ++i) {
- if (currentN % i == 0) {
- flag = true;
- break;
- }
- }
- if (!flag) {
- primeSum += currentN;
- } else {
- nonPrimeSum +=currentN;
- flag = false;
- }
- n = scan.nextLine();
- }
- if (negative) {
- System.out.println("Number is negative.");
- }
- System.out.printf("Sum of all prime numbers is: %d%n", primeSum);
- System.out.printf("Sum of all non prime numbers is: %d", nonPrimeSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement