Q:

Write a Java program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered

2

Write a Java program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
 
public class JavaLoopExcercise
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
 
        int number;
        int max = Integer.MIN_VALUE;  
        int min = Integer.MAX_VALUE;  
 
        char choice;
 
        do
        {
            System.out.print("Enter the number ");
            number = console.nextInt();
 
            if(number > max)
            {
                max = number;
            }
 
            if(number < min)
            {
                min = number;
            }
 
            System.out.print("Do you want to continue y/n? ");
            choice = console.next().charAt(0);
 
        }while(choice=='y' || choice == 'Y');
 
        System.out.println("Largest number: " + max);
        System.out.println("Smallest number: " + min);
    }  
}

Result:

Enter the number 5

Do you want to continue y/n? y

Enter the number 2

Do you want to continue y/n? y 

Enter the number 10

Do you want to continue y/n? n

Largest number: 10

Smallest number: 2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java program to print out all Armstrong nu... >>
<< Write a Java program to enter the numbers till the...