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

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


Introduction

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


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. import java.util.Scanner;
  2.  
  3. public class JavaLoopExcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int number;
  10. int max = Integer.MIN_VALUE;
  11. int min = Integer.MAX_VALUE;
  12.  
  13. char choice;
  14.  
  15. do
  16. {
  17. System.out.print("Enter the number ");
  18. number = console.nextInt();
  19.  
  20. if(number > max)
  21. {
  22. max = number;
  23. }
  24.  
  25. if(number < min)
  26. {
  27. min = number;
  28. }
  29.  
  30. System.out.print("Do you want to continue y/n? ");
  31. choice = console.next().charAt(0);
  32.  
  33. }while(choice=='y' || choice == 'Y');
  34.  
  35. System.out.println("Largest number: " + max);
  36. System.out.println("Smallest number: " + min);
  37. }
  38. }

Result

java techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.