Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter in an integer number representing

ID: 3778605 • Letter: W

Question

Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from the array that have even numbered indexes. Do not use an if statement here. Prompt the user to enter a number and count the occurrences of that number in the array. Use a separate for loop for each task. All of this work can be done in a single main () method. Sample output will be posted to D2L.

Explanation / Answer

Arrays.java

import java.util.Scanner;


public class Arrays {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the array size: ");
       int n = scan.nextInt();
       int a[] = new int[n];
       for(int i=0; i<a.length; i++){
           System.out.print("Enter the element: ");
           a[i]=scan.nextInt();
       }
       System.out.println("The array elements on single line: ");
       for(int i=0; i<a.length; i++){
           System.out.print(a[i]+" ");
       }
       System.out.println();
       System.out.println("The array elements in reverse: ");
       for(int i=a.length-1;i>=0; i--){
           System.out.print(a[i]+" ");
       }
       System.out.println();
       System.out.println("Even numbers in array are: ");
       for(int i=0; i<a.length; i++){
           if(a[i]%2 ==0)
           System.out.print(a[i]+" ");
       }
       System.out.println("Even index elements in array are: ");
       for(int i=0; i<a.length; i+=2){
           System.out.print(a[i]+" ");
       }
       System.out.println();
      
       System.out.print("Enter the number that to be searched in an array: ");
       int search = scan.nextInt();
       int count = 0;
       for(int i=0; i<a.length; i++){
           if(a[i] == search){
               count++;
           }
       }
   System.out.println("Number of occurances in ann array is "+count);
   }

}

Output:

Enter the array size: 10
Enter the element: 1
Enter the element: 2
Enter the element: 3
Enter the element: 4
Enter the element: 5
Enter the element: 1
Enter the element: 7
Enter the element: 8
Enter the element: 9
Enter the element: 10
The array elements on single line:
1 2 3 4 5 1 7 8 9 10
The array elements in reverse:
10 9 8 7 1 5 4 3 2 1
Even numbers in array are:
2 4 8 10 Even index elements in array are:
1 3 5 7 9
Enter the number that to be searched in an array: 1
Number of occurances in ann array is 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote