Advertisement
pdrenovska

Linear Data Structures ex1

May 29th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace LinearDataStructures
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     class ListNumbers
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.WriteLine("Enter some positive numbers: ");
  12.             string input = Console.ReadLine();
  13.             if (string.IsNullOrEmpty(input))
  14.             {
  15.                 return;
  16.             }
  17.  
  18.             List<int> numbers = new List<int>();
  19.  
  20.             while (!string.IsNullOrEmpty(input))
  21.             {
  22.                 int num;
  23.                 if (int.TryParse(input, out num))
  24.                 {
  25.                     if (num < 0)
  26.                     {
  27.                         throw new ArgumentException("We take only positive numbers!");
  28.                     }
  29.  
  30.                     numbers.Add(num);
  31.                 }
  32.                 else
  33.                 {
  34.                     throw new ArgumentException("Enter only positive numbers!");
  35.                 }
  36.  
  37.                 input = Console.ReadLine();
  38.             }
  39.  
  40.             decimal sum = numbers.Sum();
  41.  
  42.             Console.WriteLine("The sum is: {0}", sum);
  43.  
  44.             var average = numbers.Average();
  45.             Console.WriteLine("the average is : {0}", average);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement