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

Question 1. (Arrays.java, 20 marks) Write a program that prompts the user to ent

ID: 3778209 • Letter: Q

Question

Question 1. (Arrays.java, 20 marks) 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.

Explanation / Answer

package test;
import java.util.Scanner;
public class test1 {
   public static void main(String args[]){
       Scanner in=new Scanner (System.in);
       //asking for the number of elements in array
       System.out.println("enter total no of elements");
       int num = in.nextInt();
       int[] numbers = new int[num];
       int[] revnumbers = new int[num];
       //getting inputs for the array
       for (int i = 0; i < numbers.length; i++)
       {
       System.out.println("Please enter number");
       numbers[i] = in.nextInt();
       }
       //The values in the array on a single line.
       System.out.println("The values in the array on a single line");
       for (int i = 0; i < numbers.length; i++)
       {
           System.out.print(numbers[i]+" ");
       }
       System.out.println();
       //The array with all of the elements reversed.
       for (int i = 0; i < numbers.length; i++)
       {
           revnumbers[num-i-1]=numbers[i];
       }
       //Printing The array with all of the elements reversed.
       System.out.println("The array with all of the elements reversed");
       for (int i = 0; i < numbers.length; i++)
       {
           System.out.print(revnumbers[i]+" ");
       }
       System.out.println();
       //The values from the array that have even numbered values.
       System.out.println("The values from the array that have even numbered values.");
       for (int i = 0; i < numbers.length; i++)
       {
           if(numbers[i]%2==0)
           {
           System.out.print(numbers[i]+" ");
           }
       }
       System.out.println();
       //The values from the array that have even numbered indexes
       System.out.println("The values from the array that have even numbered indexes");
       for (int i = 0; i < numbers.length; i=i+2)
       {
           System.out.print("At index "+i+"value is "+numbers[i]+" ");
       }
       System.out.println();
       //Prompt the user to enter a number and count the occurrences of that number in the array.
       System.out.println("enter the value you wish to count");
       int value = in.nextInt();
       int count=0;
       for (int i = 0; i < numbers.length; i++)
       {
           if(numbers[i]==value)
           {
           count++;
           }
       }
       System.out.println("The value "+value+"occured "+count+"times in the array.");
   }
  
}

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