Advertisement
zlatkov

MinMaxSumAvrgProduct

Jan 13th, 2013
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 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 MinMaxSumAvrgProduct
  8. {
  9.     class MinMaxSumAvrgProduct
  10.     {
  11.         static int Min(params int[] sequence)
  12.         {
  13.             int sequenceLength = sequence.GetLength(0);
  14.             if (sequenceLength > 0)
  15.             {
  16.                 if (sequenceLength == 2)
  17.                 {
  18.                     return sequence[0] < sequence[1] ? sequence[0] : sequence[1];
  19.                 }
  20.                 else
  21.                 {
  22.                     int result = sequence[0];
  23.                     for (int i = 1; i < sequenceLength; ++i)
  24.                     {
  25.                         result = Math.Min(result, sequence[i]);
  26.                     }
  27.                     return result;
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 return 0;
  33.             }
  34.         }
  35.  
  36.         static int Max(params int[] sequence)
  37.         {
  38.             int sequenceLength = sequence.GetLength(0);
  39.             if (sequenceLength > 0)
  40.             {
  41.                 if (sequenceLength == 2)
  42.                 {
  43.                     return sequence[0] > sequence[1] ? sequence[0] : sequence[1];
  44.                 }
  45.                 else
  46.                 {
  47.                     int result = sequence[0];
  48.                     for (int i = 1; i < sequenceLength; ++i)
  49.                     {
  50.                         result = Max(result, sequence[i]);
  51.                     }
  52.                     return result;
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 return 0;
  58.             }
  59.         }
  60.  
  61.         static int Sum(params int[] sequence)
  62.         {
  63.             int sum = 0;
  64.             foreach (int x in sequence)
  65.             {
  66.                 sum += x;
  67.             }
  68.             return sum;
  69.         }
  70.  
  71.         static int Average(params int[] sequence)
  72.         {
  73.             int sum = 0, sequenceLength = sequence.GetLength(0);
  74.             foreach (int x in sequence)
  75.             {
  76.                 sum += x;
  77.             }
  78.  
  79.             return sum / sequenceLength;
  80.         }
  81.  
  82.         static int Product(params int[] sequence)
  83.         {
  84.             if (sequence.GetLength(0) > 0)
  85.             {
  86.                 int product = 1;
  87.                 foreach (int x in sequence)
  88.                 {
  89.                     product *= x;
  90.                 }
  91.  
  92.                 return product;
  93.             }
  94.             else
  95.             {
  96.                 return 0;
  97.             }
  98.         }
  99.  
  100.         static void Main(string[] args)
  101.         {
  102.             int min = Min(6, 3, 2);
  103.             int max = Max(6, 3, 2);
  104.             int sum = Sum(6, 3, 2);
  105.             int average = Average(6, 3, 2);
  106.             int product = Product(6, 3, 2);
  107.  
  108.             Console.WriteLine("Min: {0}.\nMax: {1}.\nSum: {2}.\nAverage: {3}.\nProduct: {4}.", min, max, sum, average, product);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement