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

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

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  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


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