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?