Prompt for and read a number between 1 and 5. Repeat this step until the input i
ID: 3806071 • Letter: P
Question
Prompt for and read a number between 1 and 5. Repeat this step until the input is 1 ..5. Repeat the following multiple times according to the number read in step 1. Read in a list of integers ending with a 0. The 0 marks the end of the input and is not considered part of the list Print the largest and smallest integers in the list. If only a zero appears in the list, print an error message Here is a pseudo-code solution, begin repeat prompt user to enter a number from 1 to 5 read number until number is from 1 to 5 repeat number times decrement number read input if input = 0 print error message else set min to input set max to input read input repeat while input is not= 0 if input max then set max to input end if end if read input end repeat print max print min end if end repeat end For this assignment, you are to convert the algorithm into a non-iterative solution (no loops) and code it in Java following the coding style requirements posted for the class. You may design your solution toExplanation / Answer
class GFG
{
public static void main (String []args)
{
int arr[]=new int[10];
int i=0,min,max=0;
Console con=System.console();
System.out.println("enter number between 1 to 5");
for(i=0;i<arr.length;i++)
{
arr[i]=Integer.parseInt(con.readLine());
if(arr[i]==0)
{
System.out.println("loop ended");
}
break;
}
min=arr[0];
while(arr[i]!=0)
{
if(arr[i]<min)
{
min=arr[i];
}
else
if(arr[i]>max)
{
max=arr[i];
}
i++;
}
System.out.println("minimum is:"+min);
System.out.println("maximum is:"+max);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.