Advertisement
stak441

Methods - 14 - OperationsOnIntegers

Jan 21st, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _14_15.OperationsOnIntegers
  7. {
  8.     class Program
  9.     {
  10.         static int GetMinimum(params int[] array)
  11.         {
  12.             int min = array[0];
  13.  
  14.             for (int i = 0; i < array.Length; i++)
  15.             {
  16.                 if (array[i] < min)
  17.                 {
  18.                     min = array[i];
  19.                 }
  20.             }
  21.  
  22.             return min;
  23.         }
  24.  
  25.         static int GetMaximum(params int[] array)
  26.         {
  27.             int max = array[0];
  28.  
  29.             for (int i = 0; i < array.Length; i++)
  30.             {
  31.                 if (array[i] > max)
  32.                 {
  33.                     max = array[i];
  34.                 }
  35.             }
  36.  
  37.             return max;
  38.         }
  39.  
  40.         static double GetAverage(params int[] array)
  41.         {
  42.             int sum = 0;
  43.             for (int i = 0; i < array.Length; i++)
  44.             {
  45.                 sum += array[i];
  46.             }
  47.  
  48.             return (double)sum / array.Length;
  49.         }
  50.  
  51.         static int GetSum(params int[] array)
  52.         {
  53.             int sum = 0;
  54.             for (int i = 0; i < array.Length; i++)
  55.             {
  56.                 sum += array[i];
  57.             }
  58.  
  59.             return sum;
  60.         }
  61.  
  62.         static int GetProduct(params int[] array)
  63.         {
  64.             int prod = 1;
  65.             for (int i = 0; i < array.Length; i++)
  66.             {
  67.                 prod *= array[i];
  68.             }
  69.  
  70.             return prod;
  71.         }
  72.  
  73.         static void Main(string[] args)
  74.         {
  75.             int[] array = { 1, 5, 8, -2, 6, -3 };    //Used for testing only, all methods can receive variable number of params
  76.  
  77.             int min = GetMinimum(array);
  78.             Console.WriteLine("Minimal number: {0}", min);
  79.  
  80.             int max = GetMaximum(array);
  81.             Console.WriteLine("Max number: {0}", max);
  82.  
  83.             double average = GetAverage(array);
  84.             Console.WriteLine("Average: {0}", average);
  85.  
  86.             int sum = GetSum(array);
  87.             Console.WriteLine("Sum: {0}", sum);
  88.  
  89.             int product = GetProduct(array);
  90.             Console.WriteLine("Product: {0}", product);
  91.         }
  92.  
  93.        
  94.  
  95.        
  96.        
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement