Question

Java: Write a program that prompts the user to enter integers in the range 1 to...

Java:

Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be:

10 occurs 4 times

20 occurs 2 times

25 occurs 1 time

30 occurs 1 time

40 occurs 1 time

49 occurs 1 time

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
public class TestCode {
  public static void main(String args[]) {
    int arr[] = new int[50];
    for(int i = 0;i<arr.length;i++){
      arr[i] = 0;
    }

    Scanner scanner = new Scanner(System.in);
    System.out.print("Number of integers that will be enter: ");
    int n = scanner.nextInt();

    System.out.println("Enter "+n+" numbers");
    for(int i = 0;i<n;i++){
      arr[scanner.nextInt()]+=1;
    }

    for(int i = 0;i<arr.length;i++){
      if(arr[i] > 1){
        System.out.println(i+" occurs "+arr[i]+" times");
      }
      else if(arr[i] == 1){
        System.out.println(i+" occurs "+arr[i]+" time");
      }
    }
  }
}

Output:

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Java: Write a program that prompts the user to enter integers in the range 1 to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write java program that prompts the user to enter integers from the keyboard one at a...

    Write java program that prompts the user to enter integers from the keyboard one at a time. Program stops reading integers once the user enters the same value three times consecutively. Program then outputs "Same entered 3 in a row. "

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe 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...

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a program in Java language to prompt the user to enter 3 integers (A, B,...

    Write a program in Java language to prompt the user to enter 3 integers (A, B, and C) then display these numbers from the lowest to the highest (sorted ascendingly) . Note that there are 6 different cases to handle proper order. As an example, a user entered 10 for A, 5 for B and 14 for C: the output of the program should look like this Enter number A? 10 Enter number B? 5 Enter number C? 14 Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT