lmarkov

Greatest Variable

Dec 4th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. /*
  2.  * Write a program that finds the greatest of given 5 variables.
  3. */
  4.  
  5. using System;
  6.  
  7. class GreatestVariable
  8. {
  9.     static void Main()
  10.     {
  11.         decimal[] variable = new decimal[5];
  12.         decimal greatestVariable = 0;
  13.         int i;
  14.  
  15.         string invalidInput = "Please enter a value between " + decimal.MinValue + " and " + decimal.MaxValue + Environment.NewLine;
  16.        
  17.         for (i = 0; i < 5; i++)
  18.         {
  19.             Console.WriteLine("Enter value of variable" + (i + 1) + ": ");
  20.             while (!(decimal.TryParse(Console.ReadLine(), out variable[i]) && variable[i] >= decimal.MinValue && variable[i] <= decimal.MaxValue))
  21.             {
  22.                 Console.WriteLine(invalidInput);
  23.                 Console.WriteLine("Enter value of variable" + (i + 1) + ": ");
  24.             }            
  25.         }
  26.        
  27.         for (int j = 0; j < 5; j++)
  28.         {
  29.             for (int k = 0; k < j; k++)
  30.             {
  31.                 if (variable[j] > variable[k])
  32.                 {
  33.                     greatestVariable = variable[j];
  34.                     variable[k] = variable[j];
  35.                 }
  36.                 else
  37.                 {
  38.                     greatestVariable = variable[k];
  39.                     variable[j] = variable[k];
  40.                 }
  41.             }
  42.         }
  43.         Console.WriteLine("The greatest variable is {0}" + Environment.NewLine,greatestVariable);
  44.         Main();
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment