Advertisement
veronikaaa86

04. Histogram

Jun 27th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. package forLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04Histogram {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int n = Integer.parseInt(scanner.nextLine());
  10.  
  11. double p1 = 0;
  12. double p2 = 0;
  13. double p3 = 0;
  14. double p4 = 0;
  15. double p5 = 0;
  16. for (int i = 1; i <= n; i++) {
  17. int currentNum = Integer.parseInt(scanner.nextLine());
  18.  
  19. if (currentNum < 200) {
  20. p1++;
  21. } else if (currentNum < 400) {
  22. p2++;
  23. } else if (currentNum < 600) {
  24. p3++;
  25. } else if (currentNum < 800) {
  26. p4++;
  27. } else {
  28. p5++;
  29. }
  30. }
  31.  
  32. System.out.printf("%.2f%%%n", p1 / n * 100);
  33. System.out.printf("%.2f%%%n", p2 / n * 100);
  34. System.out.printf("%.2f%%%n", p3 / n * 100);
  35. System.out.printf("%.2f%%%n", p4 / n * 100);
  36. System.out.printf("%.2f%%%n", p5 / n * 100);
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement