Java program to Sum of Even Elements

Question:

Here, size of the array should be taken as input, and it should be between 1 and 10 (inclusive) If it is not in 1 and 10, print it as “invalid” Now, by the comparing the values at the same indices of both the arrays, if both values are even, then print the sum of the two values, otherwise print “0”.

Before performing operation, make sure if no even element is present in both the arrays, then print it as “There are no even elements in both the arrays”, otherwise perform the above operation.

For example, see the following inputs and outputs:

Sample input:
Enter the size of array: 3
Enter the elements in first array:

2
3
4

Enter the elements in second array:
1
5
8

Sample output:
0
0
12

Enter the size of array: 3
Enter the elements in first array:

5
3
1
Enter the elements in second array:
1
5
9

Sample output:
There are no even elements in both the arrays

3.Sample input:
Enter the size of array: 1
Enter the elements in first array:

5
Enter the elements in second array:
1

Sample output:
There are no even elements in both the arrays

Code :

package practice;
import java.util.*;
public class SumOfEvenElements {
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	System.out.println("Enter the size of the array:");
	int n = sc.nextInt();
	if(n<=0 || n>10){
		System.out.println("Invalid");
		return;
	}
	System.out.println("Enter the elements in the first array:");
	int arr1[] = new int[n];
	for(int i = 0;i<n;i++)
		arr1[i] = sc.nextInt();
		System.out.println("Enter the elements in the second array:");
		int arr2[] = new int[n];
	for(int i = 0;i<n;i++)
		arr2[i] = sc.nextInt();
		int count = 0;
	for(int i = 0;i<n;i++){
		if(arr1[i]%2 !=0 && arr2[i]%2 != 0){
			count++;
		}
	}
		if(count == n){
			System.out.println("There are no even elements in both arrays:");
			return;
		}else{
			for(int i = 0;i<n;i++){
				if(arr1[i]%2 == 0 && arr2[i]%2 == 0){
					System.out.println(arr1[i]+arr2[i]);
				}else{
					System.out.println("0");
				}
			}
		}
	}
}
  1. OMR Evaluation
  2. Numerology Name Checking
  3. Java Program of Kids School Example
  4. Java Program of Java – Python Count
  5. Java Program of Hy-Fly Taxi
  6. Java Program for Harima Realtor’s Example

Leave a Comment

Your email address will not be published. Required fields are marked *

x