Advertisement
CR7CR7

salesF2Round

Oct 15th, 2023
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2.  
  3. public class ComputerSales
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         Console.WriteLine("Enter the number of computer models: ");
  8.         int numModels = int.Parse(Console.ReadLine());
  9.  
  10.         int totalSales = 0;
  11.         int totalRatings = 0;
  12.  
  13.         for (int i = 0; i < numModels; i++)
  14.         {
  15.             Console.WriteLine("Enter sales and rating for model {0}: ", i + 1);
  16.             int salesAndRating = int.Parse(Console.ReadLine());
  17.  
  18.             int sales = salesAndRating / 100; // possible sales
  19.             int rating = salesAndRating % 10; // rating
  20.  
  21.             totalSales += CalculateSales(sales, rating);
  22.             totalRatings += rating;
  23.         }
  24.  
  25.         double averageRating = (double)totalRatings / numModels;
  26.         double averageSales = (double)totalSales / numModels;
  27.  
  28.         Console.WriteLine("Average rating: {0:F2}", averageRating);
  29.         Console.WriteLine("Average sales: {0:F2}", averageSales);
  30.     }
  31.  
  32.     public static int CalculateSales(int sales, int rating)
  33.     {
  34.         double calculatedSales;
  35.         switch (rating)
  36.         {
  37.             case 2:
  38.                 calculatedSales = 0;
  39.                 break;
  40.             case 3:
  41.                 calculatedSales = sales * 0.5;
  42.                 break;
  43.             case 4:
  44.                 calculatedSales = sales * 0.7;
  45.                 break;
  46.             case 5:
  47.                 calculatedSales = sales * 0.85;
  48.                 break;
  49.             case 6:
  50.                 calculatedSales = sales;
  51.                 break;
  52.             default:
  53.                 calculatedSales = 0;
  54.                 break;
  55.         }
  56.  
  57.         // Round to second decimal mark using F2 rounding.
  58.         return (int)Math.Round(calculatedSales, 2);
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement