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