Advertisement
dimipan80

Number Calculations

May 8th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 KB | None | 0 0
  1. /* Write methods to calculate the minimum, maximum, average, sum and product of a given set of numbers. Overload the methods to work with numbers of type double and decimal. */
  2.  
  3. namespace _06.NumberCalculations
  4. {
  5.     using System;
  6.     using System.Globalization;
  7.     using System.Linq;
  8.     using System.Threading;
  9.  
  10.     class NumberCalculations
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  15.             int arrayType = GetTypeOfNumbersInArray();
  16.             Console.Write("Enter your sequence of ");
  17.             switch (arrayType)
  18.             {
  19.                 case 1:
  20.                     Console.WriteLine("Integer numbers on single line, separated by a space: ");
  21.                     int[] arrInts = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  22.                     Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrInts));
  23.                     Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrInts));
  24.                     Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrInts));
  25.                     Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrInts));
  26.                     Console.WriteLine("The Product of numbers in array is: {0}", GetProductOfArrayNumbers(arrInts));
  27.                     break;
  28.                 case 2:
  29.                     Console.WriteLine("Floating-point numbers on single line, separated by a space: ");
  30.                     double[] arrDoubles = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
  31.                     Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrDoubles));
  32.                     Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrDoubles));
  33.                     Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrDoubles));
  34.                     Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrDoubles));
  35.                     Console.WriteLine("The Product of numbers in array is: {0:F3}", GetProductOfArrayNumbers(arrDoubles));
  36.                     break;
  37.                 case 3:
  38.                     Console.WriteLine("Decimal numbers on single line, separated by a space: ");
  39.                     decimal[] arrDecimals = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray();
  40.                     Console.WriteLine("The Smallest number in array is: {0}", GetMinNumberOfArray(arrDecimals));
  41.                     Console.WriteLine("The Greater number in array is: {0}", GetMaxNumberOfArray(arrDecimals));
  42.                     Console.WriteLine("The Average of numbers in array is: {0:F3}", GetAverageOfArrayNumbers(arrDecimals));
  43.                     Console.WriteLine("The Sum of numbers in array is: {0}", GetSumOfArrayNumbers(arrDecimals));
  44.                     Console.WriteLine("The Product of numbers in array is: {0:F3}", GetProductOfArrayNumbers(arrDecimals));
  45.                     break;
  46.             }
  47.         }
  48.  
  49.         private static int GetTypeOfNumbersInArray()
  50.         {
  51.             Console.WriteLine(@"Please choose a type of numbers in your array:
  52. 1 --> int
  53. 2 --> double
  54. 3 --> decimal
  55. ");
  56.             int numChoice;
  57.             do
  58.             {
  59.                 Console.Write("Enter the number of your choice: ");
  60.             } while (!int.TryParse(Console.ReadLine(), out numChoice) || numChoice < 1 || numChoice > 3);
  61.  
  62.             return numChoice;
  63.         }
  64.  
  65.         private static int GetMinNumberOfArray(int[] ints)
  66.         {
  67.             int minNum = ints[0];
  68.             foreach (int number in ints)
  69.             {
  70.                 if (number < minNum)
  71.                 {
  72.                     minNum = number;
  73.                 }
  74.             }
  75.             return minNum;
  76.         }
  77.  
  78.         private static double GetMinNumberOfArray(double[] doubles)
  79.         {
  80.             double minNum = doubles[0];
  81.             foreach (double number in doubles)
  82.             {
  83.                 if (number < minNum)
  84.                 {
  85.                     minNum = number;
  86.                 }
  87.             }
  88.             return minNum;
  89.         }
  90.  
  91.         private static decimal GetMinNumberOfArray(decimal[] decimals)
  92.         {
  93.             decimal minNum = decimals[0];
  94.             foreach (decimal number in decimals)
  95.             {
  96.                 if (number < minNum)
  97.                 {
  98.                     minNum = number;
  99.                 }
  100.             }
  101.             return minNum;
  102.         }
  103.  
  104.         private static int GetMaxNumberOfArray(int[] ints)
  105.         {
  106.             int maxNum = ints[0];
  107.             foreach (int number in ints)
  108.             {
  109.                 if (number > maxNum)
  110.                 {
  111.                     maxNum = number;
  112.                 }
  113.             }
  114.             return maxNum;
  115.         }
  116.  
  117.         private static double GetMaxNumberOfArray(double[] doubles)
  118.         {
  119.             double maxNum = doubles[0];
  120.             foreach (double number in doubles)
  121.             {
  122.                 if (number > maxNum)
  123.                 {
  124.                     maxNum = number;
  125.                 }
  126.             }
  127.             return maxNum;
  128.         }
  129.  
  130.         private static decimal GetMaxNumberOfArray(decimal[] decimals)
  131.         {
  132.             decimal maxNum = decimals[0];
  133.             foreach (decimal number in decimals)
  134.             {
  135.                 if (number > maxNum)
  136.                 {
  137.                     maxNum = number;
  138.                 }
  139.             }
  140.             return maxNum;
  141.         }
  142.  
  143.         private static long GetSumOfArrayNumbers(int[] ints)
  144.         {
  145.             long sum = 0;
  146.             foreach (int number in ints)
  147.             {
  148.                 sum += number;
  149.             }
  150.             return sum;
  151.         }
  152.  
  153.         private static double GetSumOfArrayNumbers(double[] doubles)
  154.         {
  155.             double sum = 0;
  156.             foreach (double number in doubles)
  157.             {
  158.                 sum += number;
  159.             }
  160.             return sum;
  161.         }
  162.  
  163.         private static decimal GetSumOfArrayNumbers(decimal[] decimals)
  164.         {
  165.             decimal sum = 0;
  166.             foreach (decimal number in decimals)
  167.             {
  168.                 sum += number;
  169.             }
  170.             return sum;
  171.         }
  172.  
  173.         private static double GetAverageOfArrayNumbers(int[] ints)
  174.         {
  175.             return GetSumOfArrayNumbers(ints) / (double)ints.Length;
  176.         }
  177.  
  178.         private static double GetAverageOfArrayNumbers(double[] doubles)
  179.         {
  180.             return GetSumOfArrayNumbers(doubles) / doubles.Length;
  181.         }
  182.  
  183.         private static decimal GetAverageOfArrayNumbers(decimal[] decimals)
  184.         {
  185.             return GetSumOfArrayNumbers(decimals) / decimals.Length;
  186.         }
  187.  
  188.         private static long GetProductOfArrayNumbers(int[] ints)
  189.         {
  190.             long product = 1;
  191.             foreach (int number in ints)
  192.             {
  193.                 product *= number;
  194.             }
  195.             return product;
  196.         }
  197.  
  198.         private static double GetProductOfArrayNumbers(double[] doubles)
  199.         {
  200.             double product = 1;
  201.             foreach (double number in doubles)
  202.             {
  203.                 product *= number;
  204.             }
  205.             return product;
  206.         }
  207.  
  208.         private static decimal GetProductOfArrayNumbers(decimal[] decimals)
  209.         {
  210.             decimal product = 1;
  211.             foreach (decimal number in decimals)
  212.             {
  213.                 product *= number;
  214.             }
  215.             return product;
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement