Advertisement
Assi

MaxMinAvgSumProduct

Jan 15th, 2013
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class MaxMinAvgSumProduct
  5. {
  6.     static int FindMax(params int[] arr)
  7.     {
  8.         int biggestNum = arr[0];
  9.         int length = arr.Length;
  10.         for (int i = 0; i < length; i++)
  11.         {
  12.             if (length > 0)
  13.             {
  14.                 if (arr[i] > biggestNum)
  15.                 {
  16.                     biggestNum = arr[i];
  17.                 }
  18.             }
  19.             else
  20.             {
  21.                 return 0;
  22.             }
  23.         }
  24.         return biggestNum;
  25.     }
  26.  
  27.     static int FindMin(params int[] arr)
  28.     {
  29.         int smallest = arr[0];
  30.         int length = arr.Length;
  31.         for (int i = 0; i < length; i++)
  32.         {
  33.             if (length > 0)
  34.             {
  35.                 if (arr[i] < smallest)
  36.                 {
  37.                     smallest = arr[i];
  38.                 }
  39.             }
  40.             else
  41.             {
  42.                 return 0;
  43.             }
  44.         }
  45.         return smallest;
  46.     }
  47.  
  48.     static decimal FindAvg(params int[] arr)
  49.     {
  50.         decimal counter = 0;
  51.         decimal sum = 0;
  52.         decimal sumAvg = 0;
  53.         foreach (var number in arr)
  54.         {
  55.             sum = sum + number;
  56.             counter++;
  57.         }
  58.         sumAvg = sum / counter;
  59.         return sumAvg;
  60.     }
  61.  
  62.     static int FindSum(params int[] arr)
  63.     {
  64.         int sum = 0;
  65.         foreach (var number in arr)
  66.         {
  67.             sum += number;
  68.         }
  69.         return sum;
  70.     }
  71.     static int FindProduct(params int[] arr)
  72.     {
  73.         int product = 1;
  74.         foreach (var number in arr)
  75.         {
  76.             product *= number;
  77.         }
  78.         return product;
  79.     }
  80.     static void Main()
  81.     {
  82.         Console.WriteLine("Min={0}",FindMin(1, 2, 3, 4));
  83.         Console.WriteLine("Max={0}", FindMax(1, 2, 3, 4));
  84.         Console.WriteLine("Avg={0}", FindAvg(1, 2, 3, 4));
  85.         Console.WriteLine("Sum={0}", FindSum(1, 2, 3, 4));
  86.         Console.WriteLine("Product={0}", FindProduct(1, 2, 3, 4));
  87.         Console.WriteLine();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement