Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Program {
- static boolean isPrime(int num) {
- if (num < 2) {
- return false;
- }
- for (int i = 2; i * i <= num; i++) {
- if (num % i == 0) {
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int sumPrimes = 0;
- int sumNonPrimes = 0;
- while (true) {
- String input = scan.nextLine();
- if (input.equals("stop")) {
- break;
- }
- int number = Integer.parseInt(input);
- if (number > 0) {
- if (isPrime(number)) {
- sumPrimes += number;
- } else {
- sumNonPrimes += number;
- }
- } else {
- System.out.println("Number is negative");
- }
- }
- System.out.println("Sum of all prime numbers is: " + sumPrimes);
- System.out.println("Sum of all non prime numbers is: " + sumNonPrimes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement