1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mekhanik [1.2K]
3 years ago
6

in java how do i Write a program that reads a set of integers, and then prints the sum of the even and odd integers.

Computers and Technology
1 answer:
hammer [34]3 years ago
6 0

Answer:

Here is the JAVA program:

import java.util.Scanner; //to take input from user

public class Main{

public static void main(String[] args) { //start of main function

Scanner input = new Scanner(System.in); // creates Scanner class object  

       int num, integer, odd = 0, even = 0; //declare variables

       System.out.print("Enter the number of integers: "); //prompts user to enter number of integers

       num = input.nextInt(); //reads value of num from user

       System.out.print("Enter the integers:\n"); //prompts user to enter the integers

       for (int i = 0; i < num; i++) { //iterates through each input integer

           integer = input.nextInt(); // reads each integer value

               if (integer % 2 == 0) //if integer value is completely divisible by 2

                   even += integer; //adds even integers

               else //if integer value is not completely divisible by 2

                   odd += integer;  } //adds odd integers

           System.out.print("Sum of Even Numbers: " + even); //prints the sum of even integers

           System.out.print("\nSum of Odd Numbers: " + odd);//prints the sum of odd integers

           }}

Explanation:

The program is explained in the comments mentioned with each line of the code. I will explain the logic of the program with the help of an example.

Suppose user wants to input 5 integers. So,

num = 5

Suppose the input integers are:

1, 2 , 3, 4, 5

for (int i = 0; i < num; i++)  is a for loop that has a variable i initialized to 0. The condition i<num is true because i=0 and num=5 so 0<5. Hence the statements inside body of loop execute.

At first iteration:

integer = input.nextInt(); statement reads the value of input integer. The first integer is 1

if (integer % 2 == 0) checks if integer is completely divisible by 2. The modulo operator is used which returns the remainder of the division and if this remainder is equal to 0 then it means that the integer is completely divisible by 2 and if the integer is completely divisible by 2 then this means the integer is even. 1%2 returns 1 so this means this condition evaluates to false and the integer is not even. So the else part executes:

odd += integer;   this becomes:

odd = odd + integer

odd = 1

Now the value of i is incremented to 1 so i=1

At second iteration:

integer = input.nextInt(); statement reads the value of input integer. The first integer is 2

if (integer % 2 == 0) checks if integer is completely divisible by 2. 2%2 returns 0 so this means this condition evaluates to true and the integer is even. So

even += integer;   this becomes:

even= even+ integer

even = 2

Now the value of i is incremented to 1 so i=2

At third iteration:

integer = input.nextInt(); statement reads the value of input integer. The first integer is 3

if (integer % 2 == 0) checks if integer is completely divisible by 2. 3%2 returns 1 so this means this condition evaluates to false and the integer is odd. So

odd += integer;   this becomes:

odd= odd + integer

odd= 1 + 3

odd = 4

Now the value of i is incremented to 1 so i=3

At fourth iteration:

integer = input.nextInt(); statement reads the value of input integer. The first integer is 4

if (integer % 2 == 0) checks if integer is completely divisible by 2. 4%2 returns 0 so this means this condition evaluates to true and the integer is even. So

even+= integer;   this becomes:

even = even + integer

even = 2 + 4

even = 6

Now the value of i is incremented to 1 so i=4

At fifth iteration:

integer = input.nextInt(); statement reads the value of input integer. The first integer is 5

if (integer % 2 == 0) checks if integer is completely divisible by 2. 5%2 returns 1 so this means this condition evaluates to false and the integer is odd. So

odd+= integer;   this becomes:

odd= odd+ integer

odd= 4 + 5

odd = 9

Now the value of i is incremented to 1 so i=5

When i=5 then i<num condition evaluate to false so the loop breaks. The next two print statement prints the values of even and odd. So

even = 6

odd = 9

Hence the output is:

Sum of Even Numbers: 6                                                                                                           Sum of Odd Numbers: 9  

You might be interested in
mta software development fundamentals You are creating an application that accepts input and displays a response to the user. Yo
Harlamova29_29 [7]

Answer: Console-Based

Explanation:

A console based application is an application that helps in facilitating the reading and the writing of the characters from a console.

It is vital in the provision of a simple user interface for the applications that requires little interaction. Since the application accepts input and displays a response to the user and cannot create a graphical interface for this application, then it's a console based application.

8 0
2 years ago
Chang investigates ways to improve the interactivity of computer hardware. His job title is best described as ✓ Computer and Inf
xeze [42]

Answer:

B, D, C

Explanation:

got it right

3 0
2 years ago
Create a program that has at least three classes. The class with main. A class that defines a Name (first name, middle name, and
Bond [772]

Answer:

See attached file for complete detailed code.

Explanation:

See attached file.

Download txt
3 0
3 years ago
1. From where you can access save command?
Alja [10]

Answer:

1 no. ans

none of above

this ans is not 100% correct but also it help may help you

4 0
3 years ago
Why would a designer choose to use a static layout? Check all of the boxes that apply.
Dafna1 [17]

Answer:

Explanation:a & c

6 0
2 years ago
Other questions:
  • Select all that apply. Hyperlinks can appear as: words pictures symbols trademarks
    12·2 answers
  • What OS is most commonly used by businesses? Linux Macintosh Microsoft Windows
    11·1 answer
  • A list of pages within a Web site that you have visited and that usually appears at the top of a page is referred to as a(n) ___
    14·1 answer
  • What should be included in the closing portion of your letter or e-mail?
    12·2 answers
  • Hi I m from India plz follow me and I will follow u​
    15·2 answers
  • Write a java program that accepts the ingredients for a recipe in cups and converts to ounces
    10·1 answer
  • HELP I WILL MARK BRAINLIEST!!! I NEED ASAP!!!
    5·1 answer
  • According to chronology, arrange the steps that you need to take during the installation of a ram stick?
    13·1 answer
  • Why aren't my skullcandy bluetooth headphones connecting to my school chromebook if I turned on both the headphones and the blue
    10·2 answers
  • Do you think EA sports should add more tape jobs to the game? (Game is nhl22)
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!