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);
- int primeSum = 0, nonPrimeSum = 0, number;
- String input;
- while (!(input = scanner.nextLine()).equals("stop")) {
- number = Integer.parseInt(input);
- if (number >= 0) {
- if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0) {
- if (number == 2 || number == 3 || number == 5 || number == 7) {
- primeSum += number;
- } else {
- nonPrimeSum += number;
- }
- } else {
- primeSum += number;
- }
- } else {
- 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\n", nonPrimeSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement