Advertisement
clipro

c# book 5.2. Loops - Exam Problems 01. Histogram

Nov 1st, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace cshb_01_histogram
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // define variables
  14.             int r199 = 0;
  15.             int r399 = 0;
  16.             int r599 = 0;
  17.             int r799 = 0;
  18.             int r1000 = 0;
  19.  
  20.             // catch number of rows
  21.             int rows = int.Parse(Console.ReadLine());
  22.  
  23.             // for each row make a loop
  24.             for (int i = 0; i < rows; i++)
  25.             {
  26.                 int num = int.Parse(Console.ReadLine());
  27.                 if (num <= 199) r199++;
  28.                 else if (num <= 399) r399++;
  29.                 else if (num <= 599) r599++;
  30.                 else if (num <= 799) r799++;
  31.                 else r1000++;
  32.             }
  33.  
  34.             // calculate percent
  35.             double p1 = 100.00 * r199 / rows;
  36.             double p2 = 100.00 * r399 / rows;
  37.             double p3 = 100.00 * r599 / rows;
  38.             double p4 = 100.00 * r799 / rows;
  39.             double p5 = 100.00 * r1000 / rows;
  40.  
  41.             // print results
  42.             Console.WriteLine("{0:f2}%", p1);
  43.             Console.WriteLine("{0:f2}%", p2);
  44.             Console.WriteLine("{0:f2}%", p3);
  45.             Console.WriteLine("{0:f2}%", p4);
  46.             Console.WriteLine("{0:f2}%", p5);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement