Create a java program to achieve the following Ask the user

Create a java program to achieve the following:

Ask the user for the number of integers to randomly generate.

Create an integer array of the appropriate size and insert the randomly generated integers (in the range 1 – 100) into the array.

Ask the user for a search value (integer), then check to see if the array contains this value and display a message to indicate if found or not.

Finally, display the contents of the array, the sum of the numbers in the array, the smallest value and the largest value in the array.

Solution

PROGRAM CODE:

package array;

import java.util.Scanner;

public class MyMainClass {

public static void search(int[] numbers, int number)

{

   for(int i=0; i<numbers.length; i++)

   {

       if(numbers[i]==number)

       {

           System.out.println(\"\ The value \" + number +\" is present in the array\");

           return;

       }

   }

   System.out.println(\"\ The value \" + number +\" is not present in the array\");

}

  

   public static void main(String[] args) {

       int[] array;

       int size, minNum, sum = 0, maxNum = 0;

       System.out.println(\"Enter the number of Integers to store: \");

       Scanner sc = new Scanner(System.in);

       size = sc.nextInt();

       array = new int[size];

       minNum = size;

      

       for(int i=0; i<size; i++)

       {

           int num = 1 + (int)(Math.random() * 100);

           array[i] = num;

           if(minNum>num)

               minNum = num;

           if(maxNum<num)

               maxNum = num;

           sum += num;

       }

       System.out.println(\"Enter a number to search: \");

       int number = sc.nextInt();

       search(array, number);

      

       System.out.println(\"\ Printing the contents of the array: \ \");

       //printing the contents of the array

       for(int j=0; j<array.length; j++)

       {

           System.out.print(array[j] + \" \");

           if((j+1)%10 == 0)

               System.out.println(\"\");

       }

       //printing to screen

       System.out.println(\"\ The sum of numbers is \"+ sum);

       System.out.println(\"The smallest number is \" + minNum);

       System.out.println(\"The largest number is \" + maxNum);

      

   }

}

OUTPUT:

Enter the number of Integers to store:

30

Enter a number to search:

23

The value 23 is not present in the array

Printing the contents of the array:

85 14 36 49 11 59 34 33 68 78

93 86 44 24 20 18 63 92 9 95

60 10 47 31 26 81 80 60 4 83

The sum of numbers is 1493

The smallest number is 4

The largest number is 95

Create a java program to achieve the following: Ask the user for the number of integers to randomly generate. Create an integer array of the appropriate size an
Create a java program to achieve the following: Ask the user for the number of integers to randomly generate. Create an integer array of the appropriate size an
Create a java program to achieve the following: Ask the user for the number of integers to randomly generate. Create an integer array of the appropriate size an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site