Advertisement
ntodorova

03_MinimalAndMaximal

Nov 11th, 2012
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.  * 3. Write a program that reads from the console a sequence of N integer
  5.  * numbers and returns the minimal and maximal of them.
  6.  */
  7. class MinimalAndMaximal
  8. {
  9.     static void Main()
  10.     {
  11.         int n;
  12.         int num;
  13.         int min = int.MinValue;
  14.         int max = int.MaxValue;
  15.  
  16.         Console.WriteLine("How many numbers you will enter?");
  17.         string str = Console.ReadLine();
  18.  
  19.         if (!int.TryParse(str, out n))
  20.         {
  21.             Console.WriteLine("Invalid number: {0}", str);
  22.         }
  23.         else
  24.         {
  25.             for (int i = 1; i <= n; i++)
  26.             {
  27.                 Console.WriteLine("Please enter a number:");
  28.                 string strEnter = Console.ReadLine();
  29.  
  30.                 if (!int.TryParse(strEnter, out num))
  31.                 {
  32.                     Console.WriteLine("Invalid number: {0}", strEnter);
  33.                 }
  34.                 else
  35.                 {
  36.                     if (i == 1)
  37.                     {
  38.                         min = num;
  39.                         max = num;
  40.                     }
  41.  
  42.                     if (num < min)
  43.                     {
  44.                         min = num;
  45.                     }
  46.  
  47.                     if (num > max)
  48.                     {
  49.                         max = num;
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             Console.WriteLine("The min number is: {0}", min);
  55.             Console.WriteLine("The max number is: {0}", max);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement