Q:

Write a Java program that reads a set of integers, and then prints the sum of the even and odd integers using loop

0

Write a Java program that reads a set of integers, and then prints the sum of the even and odd integers using loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
 
public class JavaLoopExcercise
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
 
        int number;
        char choice;
        int even = 0;
        int odd = 0;
 
        do
        {
            System.out.print("Enter any number ");
            number = console.nextInt();
 
            if( number % 2 == 0)
            {
                even += number;
            }
            else
            {
                odd += number;
            }
 
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
 
        }while(choice=='y' || choice == 'Y');
 
        System.out.println("Sum of even numbers: " + even);
        System.out.println("Sum of odd numbers: " + odd);
    }  
}

Result:

Enter any number 5

Do you want to continue y/n? y

Enter any number 4

Do you want to continue y/n? y

Enter any number 6

Do you want to continue y/n? y

Enter any number 3

Do you want to continue y/n? n

Sum of even numbers: 10

Sum of odd numbers: 8

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to check whether the number i... >>
<< Write a Java program that prompts the user to inpu...