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
Select the strategies below that are likely to increase audience attention.
anyanavicka [17]
Vary the use of transitions for each slide
5 0
3 years ago
Read 2 more answers
Write a function named firstLast2 that takes as input an array of integers and an integer that specifies how many entries are in
frosja888 [35]

Answer:

<em>Written in C++</em>

#include<iostream>

using namespace std;

int firstLast2(int arr[], int n);

int main()

{

int n;

cout<<"Number of Elements: ";

cin>>n;  

int myarray[n];

for(int i = 0; i<n;i++) {

cin>>myarray[i];

}

firstLast2(myarray, n);

return 0;

}

int firstLast2(int arr[], int n){

if(arr[0] == 2 || arr[n - 1] == 2) {

 cout<<"True";

}

else {

cout<<"False";

}

}

Explanation:

<em>I've added the full source code as an attachment where I used comments to explain difficult lines</em>

Download cpp
4 0
3 years ago
Can someone help me with...A table can help? PLEASE
algol [13]

Answer:

A table cannot help people

3 0
3 years ago
What is working with others to find a mutually agreeable outcome?
Arturiano [62]

Answer:

its B. negotiation

Explanation:

1. I got it right on the exam :)

2. negotiation resolves in problem solving. you work together with others to solve problems, together which is negotiating/ talking things through to figure out an answer/ outcome.

Hope this helps! :))

Have an awesome day!!

3 0
3 years ago
How many valence electrons are present in the atom of the atomic number of 12?
Natali [406]
When we do the electronic configuration of the atom we get that on K shell the no. Of atoms is 2 in L shell the no. Of atoms will be 8 and in M shell or the valence shell the no. Of atoms will be 2 so the no. Of valence electrons will be 2.

5 0
3 years ago
Other questions:
  • ?the single most effective security measure for digital devices is to password protect access to them.
    5·1 answer
  • Ben's team is working on an English project. The members want to see everyone's progress on their part of the project. What tech
    7·2 answers
  • Linux would be a good example of?
    6·1 answer
  • Give an example of an app, website, or a game that used addictive design. Describe three features of addictive design it uses.
    11·2 answers
  • Write a function that will alphabetize a string WITHOUT using the sort function :
    7·1 answer
  • What is a CPU made of
    13·2 answers
  • c724 wgu True or false. A storage device consists of all the components that work together to process data into useful informati
    10·1 answer
  • Only for study<br><br>Or open Meet and enter this code: bqa-ivfs-ach​
    11·2 answers
  • When would the Jerusalem virus attack?<br> 1. Friday the 13th<br> 2. Wednesday the 13th
    13·2 answers
  • Imagine running a 64-bit system on a 32-bit system, where we simulate a single 64- bit memory location (register) using two atom
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!