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

This program will require you to: Ask user to enter the length of the array Read

ID: 3831418 • Letter: T

Question

This program will require you to:

Ask user to enter the length of the array

Read in that length and use it to create an array of integers

Write a loop that asks the user to enter an integer and then reads it into the array

Write a loop that finds the max of the array

Write a loop to find the min of the array

Write a loop to find the average of the array(be careful with int division)

Write a loop to find the number of numbers in array divisible by 5

Write a loop to print the array

Write a loop to print the array backwards

Find the smallest two numbers in the array

Example output

----jGRASP exec: java Loop Enter array length
7
Enter value

21
Enter value 23
Enter value 56
Enter value

76
Enter value 89
Enter value 45
Enter value 34
The max is 89

The min is 21
The average is 49.142857142857146
The number of numbers divisible by 5 is is 1
The array forward is 21 , 23 , 56 , 76 , 89 , 45 , 34 , The array reversed is 34 , 45 , 89 , 76 , 56 , 23 , 21 , The smallest two numbers are 21 and 23

You can get 1 extra credit point if you figure out how to not have the last number be followed by a ,

** Do not use any java imports except import java.util.Scanner;

Explanation / Answer

Here is the program for all requirements:

In this program first, we sort the array in ascending order then using this we get 2 minimum numbers, average, and descending order, minimum value and maximum values.

Program:

import java.util.Scanner;

public class loop {

   static void Findmax(int arr[])
   {
   for (int i=0;i<arr.length ;i++)
   {
       for(int j = i+1 ; j <arr.length ; j++)
       {
           if(arr[i]>arr[j])
           {
               int temp = arr[i];
               arr[i]=arr[j];
               arr[j]=temp;
           }
       }
   }
   int n = arr.length-1;
   System.out.println("Minimum Value is: "+ arr[0]);
   System.out.println("Maximum Value is: "+ arr[n]);
  
  
  
   int sum = 0;
   double avg = 0;
   for(int k = 0 ; k < arr.length ; k ++)
   {
       sum+=arr[k];
   }
   avg = (double)sum / arr.length;
   System.out.println("The average is: "+ avg);
  
   System.out.print("The array Forword is : ");
   for(int k = 0 ; k < arr.length ; k ++)
   {
       System.out.print( arr[k] + ",");
   }
   System.out.println();
  
   System.out.print("The array reversed is : ");
   for(int k = arr.length-1 ; k >=0 ; k --)
   {
       System.out.print( arr[k] + ",");
   }
   System.out.println();
  
   System.out.println("The smallest two numbers are: " + arr[0] + " and " + arr[1]);
  
  
   }
  
   static void count(int arr[])
   {
       int count = 0;
       for(int k = 0 ; k < arr.length ; k ++)
       {
           if(arr[k]%5 == 0)
               count++;
       }
       System.out.println("The number of numbers divisible by 5 is: "+ count);
   }
  

public static void main(String[] args)
{
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter The Size Of Array: ");
   int n= sc.nextInt();
   int arr[] = new int[n];
   System.out.println("Enter The input numbers for array :");
   for(int i=0 ; i < n ; i++)
   {
       arr[i] = sc.nextInt();
   }
      
   Findmax(arr);
   count(arr);
}      
}

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