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

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  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




Popular Posts