Write a program in javato store 20 numbers (including even and odd numbers) in a single dimensional array and display the sum of all even and all odd numbers saperately stored in the cell
Answers
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[20];
int i,odd=0,even=0,b;
for(i=0;i<20;i++) {
a[i]= sc.nextInt();
if(a[i]%2==0)
even+=a[i];
else
odd+=a[i];
}
System.out.println("Sum of even numbers: "+even);
System.out.println("Sum of odd numbers: "+odd);
}
}
Answer:
import java.util.Scanner; public class Program
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=new int[20]; for(i=0;i<20;i++) { a[i]= sc.nextInt();
int i,odd=0,even=0,b;
if(a[i]%2==0)
even+=a[i];
else
odd+=a[i];
}
System.out.println("Sum of even numbers:
"+even);
System.out.println("Sum of odd numbers:
"+odd);
}
}