68 758
Assignments Done
Successfully Done
In January 2019

Answer to Question #70365 in Java | JSP | JSF for atif haroon

Question #70365
Write a program that reads in a list of integers from the user until the user enters the value 0 as
a sentinel. When the sentinel appears, your program should display the largest and second largest
value in the list, e.g. if user enter values 13, 17, 34, 19, 33,0 then program output should say the
largest value was 34 and second largest value was 33. You should also make sure that the program
works correctly if all the input values are negative. not using arrays
Expert's answer
import java.util.Scanner;
public class JavaApplicationLargestSecondlargest {

public static void main(String[] args) {
System.out.println("enter interges, the value 0 as a sentinel ");
Scanner scan = new Scanner(System.in);
int num;
int max = Integer.MIN_VALUE;
int maxsecond = Integer.MIN_VALUE;
num = scan.nextInt();
if (num == 0) continue;

if (max < num){
maxsecond = max;
max = num;
else if (maxsecond < num)
maxsecond = num;
}while (num != 0);
System.out.println("largest value was " + max + " and second largest value was " + maxsecond);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions