write a program to initialise 20 integers in an array[] print the array horizontally with one is space between each number at the end print sum of all even integers and sum of odd integers from the array
Answers
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[20];
int evenSum = 0 , oddSum = 0;
int n = arr.length;
System.out.println("Enter all 20 numbers in the array");
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
if(arr[i]%2 == 0){
evenSum += arr[i];
}
else if(arr[i]%2 == 1){
oddSum += arr[i];
}
}
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println("\nthe sum of all even numbers in the array is " + evenSum);
System.out.println("the sum of all odd numbers in the array is " + oddSum);
}
}
Explanation: