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);
- String command = scanner.nextLine();
- Integer sumPrime = 0;
- Integer sumNonPrime = 0;
- while(!"stop".equals(command)){
- Integer number = Integer.parseInt(command);
- Integer counter = 0;
- if(number < 0){
- System.out.println("Number is negative.");
- }else{
- for(int i = 1; i <= number; i++){
- if(number % i == 0){
- counter += 1;
- }
- }
- if(counter == 2){
- sumPrime += number;
- }else{
- sumNonPrime += number;
- }
- }
- command = scanner.nextLine();
- }
- System.out.printf("Sum of all prime numbers is: %d\n", sumPrime);
- System.out.printf("Sum of all non prime numbers is: %d", sumNonPrime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement