Write a Java application that allows a user to enter numbers

Write a Java application that allows a user to enter numbers into an array and then process them.

Enter the number of elements and the value of each element;

Your application will process the data entered and will display the results:

Display the elements of the array, max 10 per line

Find and display the min value and the index of this min;

Display the odd elements;

Sorts and displays the numbers from highest to lowest using Arrays.sort() method from java.util.Arrays class and some extracode.

Solution

PrintArray.java


import java.util.Arrays;
import java.util.Scanner;

public class PrintArray {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the number of elements: \");
       int n = scan.nextInt();
       int values[] = new int[n];
       for(int i=0; i<values.length; i++){
           System.out.print(\"Enter the value: \");
           values[i] =scan.nextInt();
       }
       System.out.println(\"Array elements are: \");
       for(int i=0; i<values.length; i++){
       if(i % 10 == 0){
       System.out.println();  
       }
       System.out.print(values[i]+\" \");
       }
       int min = values[0];
       int minIndex = 0;
       for(int i=0; i<values.length; i++){
           if(min > values[i]){
               min = values[i];
               minIndex = i;
           }
       }
       System.out.println();
       System.out.println(\"Min value is \"+min);
       System.out.println(\"Min index is \"+minIndex);
       System.out.println(\"Odd elements are: \");
       for(int i=0; i<values.length; i++){
           if(values[i] % 2 != 0)
       System.out.print(values[i]+\" \");
       }
       System.out.println();
       Arrays.sort(values);
      
       System.out.println(\"Array elements are after sorting: \");
       for(int i=values.length-1; i>=0; i--){
      
           System.out.print(values[i]+\" \");
           }
       System.out.println();
          
   }

}

Output:

Enter the number of elements: 15
Enter the value: 11
Enter the value: 12
Enter the value: 13
Enter the value: 14
Enter the value: 15
Enter the value: 1
Enter the value: 1
Enter the value: 2
Enter the value: 3
Enter the value: 4
Enter the value: 5
Enter the value: 6
Enter the value: 7
Enter the value: 8
Enter the value: 9
Array elements are:

11 12 13 14 15 1 1 2 3 4
5 6 7 8 9
Min value is 1
Min index is 5
Odd elements are:
11 13 15 1 1 3 5 7 9
Array elements are after sorting:
15 14 13 12 11 9 8 7 6 5 4 3 2 1 1

Write a Java application that allows a user to enter numbers into an array and then process them. Enter the number of elements and the value of each element; Yo
Write a Java application that allows a user to enter numbers into an array and then process them. Enter the number of elements and the value of each element; Yo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site