Advertisement
GabrielHr00

Histogram

Nov 20th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. package S4_ForLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Histogram {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int n = Integer.parseInt(scanner.nextLine());
  9.  
  10. // define counters for each diapason
  11. int p1Count = 0;
  12. int p2Count = 0;
  13. int p3Count = 0;
  14. int p4Count = 0;
  15. int p5Count = 0;
  16.  
  17. for (int i = 0; i < n; i++) {
  18. int number = Integer.parseInt(scanner.nextLine());
  19.  
  20. // check for defined diapason and save count for each
  21. if (number < 200) {
  22. // p1Count = p1Count + 1;
  23. // p1Count += 1;
  24. p1Count++;
  25. } else if(number <= 399) {
  26. p2Count++;
  27. } else if(number <= 599) {
  28. p3Count++;
  29. } else if(number <= 799) {
  30. p4Count++;
  31. } else {
  32. p5Count++;
  33. }
  34. }
  35.  
  36. System.out.printf("%.2f%%%n", 1.0 * p1Count/n * 100);
  37. System.out.printf("%.2f%%%n", 1.0 * p2Count/n * 100);
  38. System.out.printf("%.2f%%%n", 1.0 * p3Count/n * 100);
  39. System.out.printf("%.2f%%%n", 1.0 * p4Count/n * 100);
  40. System.out.printf("%.2f%%", 1.0 * p5Count/n * 100);
  41.  
  42. }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement