Advertisement
yovkovbpfps

For Loop Exercise Histogram

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