English

Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array (SDA). Calculate and display the sum of all even numbers and all odd numbers separately. - Computer Applications

Advertisements
Advertisements

Question

Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array (SDA). Calculate and display the sum of all even numbers and all odd numbers separately.

Answer in Brief

Solution

import java.util.*;
public class AQ1
{ public static void main(String] jp{
Scanner ob = new Scanner(System.in);
int []n = new int[6];
int even = 0, odd = 0;
System.out.println("Enter 20 numbers");

// INPUT AND ADDITION
for (int i = 0; i < n.length; i++)
{n[i] = ob.nextInt();
if (n[i] % 2 == 0)
even += n[i];
else
odd += n[i];
}

//OUTPUT
System.out.println("Sum of even numbers = " + even);
System.out.printIn("Sum of even numbers = " + odd);
}}
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 240]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 3 Arrays (Single Dimensional and Double Dimensional)
EXERCISES | Q VII. 1. | Page 240
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×