Java program that read a sequence of integer inputs and print the number of even and odd inputs.
Posted by Samath
Last Updated: April 03, 2022

Write a program that read a sequence of integer inputs and print the number of even and odd inputs.

Code:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
		   Scanner input = new Scanner(System.in);
	       
	       double num;
	       int odd_numbers = 0;
	       int even_numbers = 0;
	       
	        System.out.print("Enter Number (-1 to end input): ");
	       num = input.nextDouble();
	       
	       
	       while (num != -1) {
	           
	           if (num % 2 == 0) {
		          even_numbers += 1;
	           }
	           else {
		          odd_numbers += 1;
	            }
	            
	            System.out.print("Enter Number (-1 to end input): ");
	            num = input.nextDouble();
	            
	        }
	       input.close();
	       
	        System.out.println("Odd numbers: " + odd_numbers);
	        System.out.println("Even numbers: " + even_numbers);
	}
}
Related Content