Write a Java program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros


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.  
  2. import java.util.Scanner;
  3.  
  4. public class JavaLoopExcercise
  5. {
  6. public static void main(String[] args)
  7. {
  8. Scanner console = new Scanner(System.in);
  9.  
  10. int number,
  11. countPositive = 0,
  12. countNegative = 0,
  13. countZero = 0;
  14.  
  15. char choice;
  16.  
  17. do
  18. {
  19. System.out.print("Enter the number ");
  20. number = console.nextInt();
  21.  
  22. if(number > 0)
  23. {
  24. countPositive++;
  25. }
  26. else if(number < 0)
  27. {
  28. countNegative++;
  29. }
  30. else
  31. {
  32. countZero++;
  33. }
  34.  
  35. System.out.print("Do you want to continue y/n? ");
  36. choice = console.next().charAt(0);
  37.  
  38. }while(choice=='y' || choice == 'Y');
  39.  
  40. System.out.println("Positive numbers: " + countPositive);
  41. System.out.println("Negative numbers: " + countNegative);
  42. System.out.println("Zero numbers: " + countZero);
  43. }
  44. }

Result

java techstudy.org

     





Related Topics




Popular Posts