34.5k views
2 votes
Write a program that prompts the user to enter a number then counts the number of odd numbers and even numbers the user enter. The program should exit if the user enter a zero or a number less than zero. For example, if the user enters 4, 7, 9, 8 21, 22, -1; upon entering -1 the program displays/output "3 even numbers and 3 odd numbers" were entered, then the program terminate

User Ajdams
by
7.6k points

1 Answer

2 votes

Answer:

The program written in Java is given in the explanation section

Step-by-step explanation:

import java.util.Scanner;

public class num3 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("This program counts evens and odds");

System.out.println("To exit enter ZERO or a NEGATIVE number");

int num = in.nextInt();

int numEvens=0;

int numOdds = 0;

while(num>=1){

if(num%2==0){

numEvens++;

}

else{

numOdds++;

}

System.out.println("Enter the next number");

num = in.nextInt();

}

System.out.println(numEvens+" even numbers and "+numOdds+" odd numbers");

}

}

User Miryam
by
7.7k points