This Java program allows the user to enter an array of integers and calculates the sum of all the odd numbers in the array. Here is how the program works:
import java.util.Scanner; class Array_Sum_OddNum { public static void main(String[] args) { Scanner input =new Scanner(System.in); System.out.print("Enter the Array Limit :"); int l =input.nextInt(); int [] a =new int[l]; int sum = 0; for(int i=0;i<l;i++) { System.out.printf("Element of a[%d] :",i); a[i]=input.nextInt(); } for(int o:a) { if(o%2==1) sum = sum + o; } System.out.println("Sum of Odd Array Elements : "+sum); } }
Enter the Array Limit :5 Element of a[0] :12 Element of a[1] :34 Element of a[2] :59 Element of a[3] :45 Element of a[4] :22 Sum of Odd Array Elements : 104
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions