A program (Java) that outputs the sum of odd and even numbers in an array

About the procedure

This is a program that finds the sum of odd numbers and the sum of even numbers in an array and displays the result. Follow the procedure below.

  1. Define variables to manage an array of numbers
  2. Define variables to manage even and odd numbers
  3. Describe the iterative process with a for statement
  4. Describe odd and even conditional branches in the if statement
  5. Description for outputting the result

Define variables to manage an array of numbers

Main.java


class Main {
 public static void main(String[] args) {
   //Substitute the given array of numbers for the variable numbers
   int[] numbers = {1, 4, 6, 9, 13, 16};
 }
}

Define variables to manage even and odd numbers

Main.java


class Main {
 public static void main(String[] args) {
   int[] numbers = {1, 4, 6, 9, 13, 16};
   //Define the variable oddSum and assign 0(This is odd)
   int oddSum = 0;
   //Define the variable evenSum and assign 0(This is an even number)
   int evenSum = 0;
 }
}

Describe iterative processing with a for statement

As a processing procedure, while issuing all the arrays with the for statement, conditional branching is done with the if statement. By writing as follows, each element of the array numbers is assigned to the variable number in order.

Main.java


class Main {
 public static void main(String[] args) {
   int[] numbers = {1, 4, 6, 9, 13, 16};
   int oddSum = 0;
   int evenSum = 0;
    //Repeat array numbers using for statement
   for (int number : numbers) {
  }
 }
}

Describe odd and even conditional branches in if statement

Let the description of (number% 2 == 0) determine whether it is an even number.

Main.java


class Main {
 public static void main(String[] args) {
   int[] numbers = {1, 4, 6, 9, 13, 16};
   int oddSum = 0;
   int evenSum = 0;
   for (int number : numbers) {
    //Describe conditional branch in if statement
     if (number % 2 == 0) {
      evenSum += number;
   } else {
      oddSum += number;
   }
  }
 }
}

Description for outputting the result

Describe it outside the for statement. If you write it in the for statement, it will be output every time you call each element of numbers.

Main.java


class Main {
 public static void main(String[] args) {
   int[] numbers = {1, 4, 6, 9, 13, 16};
   int oddSum = 0;
   int evenSum = 0;
   for (int number : numbers) {
    //Describe conditional branch in if statement
     if (number % 2 == 0) {
      evenSum += number;
   } else {
      oddSum += number;
   }
  }
  //Described outside the for statement
  System.out.println("The sum of odd numbers is" + oddSum + "is");
  System.out.println("The sum of even numbers" + evenSum + "is");
 }
}

That's it.

Recommended Posts

A program (Java) that outputs the sum of odd and even numbers in an array
Sample program that returns the hash value of a file in Java
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
A program that counts the number of words in a List
[Ruby] How to count even or odd numbers in an array
Cast an array of Strings to a List of Integers in Java
A program that outputs a number greater than or equal to the input integer and a number less than the input integer from an array with 50 elements.
Compare the elements of an array (Java)
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
This and that of the implementation of date judgment within the period in Java
Find the maximum and minimum of the five numbers you entered in Java
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
Measure the size of a folder in Java
Feel the passage of time even in Java
Output the sum of each name and its contents from a multiple array
[Java] Program example to get the maximum and minimum values from an array
Calculate the difference between numbers in a Ruby array
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
[Ruby] Count an even number in an array using the even? Method
Determine that the value is a multiple of 〇 in Ruby
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
A method that applies transforms in parallel to all elements and returns an array of return values that maintain their order
Java beginners briefly summarized the behavior of Array and ArrayList
Java program that outputs Shift-JIS format in UTF-8 format at once
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
Generate Stream from an array of primitive types in Java
[Java] How to search for a value in an array (or list) with the contains method
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
[Java] Get the dates of the past Monday and Sunday in order
[Swift5] How to get an array and the complement of arrays
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
[Java] Declare and initialize an array
This and that of the JDK
Order of processing in the program
A memo that experimented whether a thread that ran out of control in an infinite loop in Java could be forcibly stopped from the outside
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
Link Apache and Tomcat in a blink of an eye on CentOS 8
A note on the differences between interfaces and abstract classes in Java
A program that determines whether the entered integer is close to an integer
How to get the absolute path of a directory running in Java
I want to ForEach an array with a Lambda expression in Java
Review of "strange Java" and Java knowledge that is often forgotten in Java Bronze
[Java] Implement a function that uses a class implemented in the Builder pattern
When seeking multiple in a Java array
Get the result of POST in Java
Map without using an array in java
[Ruby] A program that determines prime numbers
Organized memo in the head (Java --Array)
Convert an array of strings to numbers
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
Program PDF headers and footers in Java
The story of writing Java in Emacs
Discrimination of Enums in Java 7 and above