This Java program takes user input for the size of an array, then prompts the user to enter the elements of the array. It then finds and prints all the odd elements of the array. Here is how the program works:
Note that this program assumes that the user will only input integers, and it does not handle any potential exceptions or errors. Also, it's a good practice to close the Scanner object after use to avoid memory leaks. However, it's not required in this program as the Scanner object is only used within the main method and will be automatically garbage collected after the program ends.
import java.util.Scanner; class Array_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]; for(int i=0;i<l;i++) { System.out.printf("Element of a[%d] :",i); a[i]=input.nextInt(); } System.out.println("\nOdd Array Elements...\n"); for(int o:a) { if(o%2==1) System.out.println(o); } } }
Enter the Array Limit :5 Element of a[0] :23 Element of a[1] :45 Element of a[2] :67 Element of a[3] :34 Element of a[4] :78 Odd Array Elements... 23 45 67
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions