# Java coding examples for beginners

By John

This is a Java Program to Calculate the Sum of Odd & Even Numbers.
Enter the number of elements you want in array. Now enter all the elements you want in that array. We begin from the first element and check if it is odd or even. Hence we add that number into the required addition list dependng whether it is even or odd.

Here is the source code of the Java Program to Calculate the Sum of Odd & Even Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;
public class Sum_Odd_Even
{
public static void main(String[] args)
{
int n, sumE = 0, sumO = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the number of elements in array:”);
n = s.nextInt();
int[] a = new int[n];
System.out.println(“Enter the elements of the array:”);
for(int i = 0; i < n; i++) { a[i] = s.nextInt(); } for(int i = 0; i < n; i++) { if(a[i] % 2 == 0) { sumE = sumE + a[i]; } else { sumO = sumO + a[i]; } } System.out.println("Sum of Even Numbers:"+sumE); System.out.println("Sum of Odd Numbers:"+sumO); } } Output: \$ javac Sum_Odd_Even.java \$ java Sum_Odd_Even Enter the number of elements in array:6 Enter the elements of the array: 1 3 2 6 7 9 Sum of Even Numbers:8 Sum of Odd Numbers:20