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

A certain engineering apparatus is controlled by the input of successive numbers

ID: 3537047 • Letter: A

Question

A certain engineering apparatus is controlled by the input of successive numbers. If there is a run of the same number, the apparatus can optimize its performance. Write a program that reads a sequence of numbers and prints out each run of numbers in the form m(n), where m is the number which occurred n times in succession. The numbers will be in increasing order. A number which is less than the previous number will terminate the input.

Here's a sample of how the program should run (user input is shown as bold italics):

-5

-5

-5

2

-5(3)

2

2

2

10

2(4)

10

11

10(2)

7

11(1)

In the sample run, we can see that three -5's were entered, followed by a 2. When the program detected that the run of -5's was completed, it reported -5(3), which means that there was a run of three -5's. Note: the program couldn't report the first run until a number which didn't belong to the run had been entered. This is not a design flaw -- it's the way your program should work!

More 2's were entered. When the program detected that the run of 2's was completed, it reported 2(4), and so on. The program stopped when the number sequence decreased -- ie. 7 is less than 11.

Your solution should use a single while loop. As explained above, the program should continue to run "while the last number entered is not less than the previous number".

Explanation / Answer

import java.util.*;

public class Guessing

{


public static void main (String[ ] args)

{


int num,prenum;

boolean b=false;

int i=0;

Scanner scan = new Scanner (System.in);

prenum = scan.nextInt();int count=1;

while(true)

{

num = scan.nextInt();

if(prenum>num)

{System.out.println(prenum+"("+count+")");break;}

else if(prenum==num)

{

count++;prenum=num;

}

else

{

System.out.println(prenum+"("+count+")");

count=1;

prenum=num;

}



}}}

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