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

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.

Solution

import java.util.Scanner;


public class ArraysTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       int size;
       Scanner scan= new Scanner(System.in);
       System.out.println(\"Please enter an integer for the number of elements in your array\");
       size=scan.nextInt();
      
       //array declaration of size size
       int arr[]= new int[size];
      
       //loop to enter the value to the array
       for(int i=0;i<arr.length;i++){
           System.out.print(\"Please enter the value for element number \"+i+\": \");
           arr[i]=scan.nextInt();
       }
      
       //loop to print the array in single line
       System.out.println(\"Printing the array in order\");
       for(int i=0;i<arr.length;i++){
           System.out.print(arr[i]+\" \");
       }
       System.out.print(\"\ \");
      
       //reversing an Array
       System.out.println(\"Printing the array reversed\");
       rvereseArray(arr, 0, arr.length-1);
       for(int i=0;i<arr.length;i++){
           System.out.print(arr[i]+\" \");
       }
       System.out.print(\"\ \");
      
       //print even numbers from the array
       System.out.println(\"Printing the even values\");
       for(int i=0;i<arr.length;i++){
           if(arr[i]%2==0)
               System.out.print(arr[i]+\" \");
       }
       System.out.print(\"\ \");
      
       //printing the array from the even indexed
       System.out.println(\"Printing the values with even index (including 0 as even)\");
       for(int i=0;i<arr.length;i+=2){
           System.out.print(arr[i]+\" \");
       }
       System.out.print(\"\ \");
      
       System.out.print(\"Please enter a value to count the occurence:\");
       int occ=scan.nextInt();
       int count=0;
       for(int i=0;i<arr.length;i++){
           if(arr[i]==occ)
               count++;
       }
       System.out.println(\"There are \"+count+\" occurences of \"+occ);
   }

   private static void rvereseArray(int[] arr, int start, int end) {
       int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseArray(arr, start+1, end-1);
      
   }

}
------output------
Please enter an integer for the number of elements in your array
9
Please enter the value for element number 0: 1
Please enter the value for element number 1: 2
Please enter the value for element number 2: 3
Please enter the value for element number 3: 4
Please enter the value for element number 4: 5
Please enter the value for element number 5: 6
Please enter the value for element number 6: 7
Please enter the value for element number 7: 8
Please enter the value for element number 8: 9
Printing the array in order
1 2 3 4 5 6 7 8 9
Printing the array reversed
9 8 7 6 5 4 3 2 1
Printing the even values
8 6 4 2
Printing the values with even index (including 0 as even)
9 7 5 3 1
Please enter a value to count the occurence:6
There are 1 occurences of 6
---------------

//Note: feel free to ask doubts. God bless you

 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 us
 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 us

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site