asked 222k views
4 votes
Write a program that reads an arbitrary number of integer that are in the range 0 - 50 inclusive and counts how many occurrences of each are entered. Indicate the end of the input by a value outside of the range. After all input has been processed, print all of the values (with the number of occurences) that were entered one or more times.

1 Answer

5 votes

Answer:

Check the explanation

Step-by-step explanation:

import java.util.Scanner;

public class Count

{

public static void main(String[] args)

{

// creating array of store to store count of

//every integer entered

int[] store = new int[51];

System.out.println("Enter any integers between"+

" 0 and 50, and if you are done then enter any"+

" integer out of the range");

System.out.println("Enter Integer: ");

//taking input of first integer

Scanner scan = new Scanner(System.in);

int integer = scan.nextInt();

//looping for integers entered in range

//to count its occurance

while(integer >= 0 && integer <= 51)

{

//adding count to 'store' array of that

//particular array of value entered

store[integer] = store[integer] + 1;

System.out.println("Enter Integer: ");

integer = scan.nextInt();

}

//printing number of times each integer is entered

System.out.println("printing number of times"+

" each integer is entered");

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

{

if (store[i] > 0)

System.out.println(i + ": " + store[i]);

}

}

}

Kindly check the result below

Write a program that reads an arbitrary number of integer that are in the range 0 - 50 inclusive-example-1
answered
User Brad Wickwire
by
8.5k points