Advertisement
milen8204

Problem 7. Sum of 5 Numbers

Mar 19th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2. /* Write a program that enters 5 numbers (given in a single line, separated by a space),
  3.  * calculates and prints their sum. Examples:
  4.  * numbers              sum    
  5.  * 1 2 3 4 5            15
  6.  * 10 10 10 10 10       50
  7.  * 1.5 3.14 8.2 -1 0    11.84
  8.  */
  9. class SumNumbersInLine
  10. {
  11.     static void Main()
  12.     {
  13.         Console.WriteLine("Please, enter five numbers in line separated with space and hit \"Еnter\":");
  14.         string input = Console.ReadLine();
  15.         string[] numbers = input.Split(' ');
  16.         double sumNumbers = 0;
  17.         Console.Clear();
  18.        
  19.         try
  20.         {
  21.             foreach (var item in numbers)
  22.             {
  23.                 sumNumbers = sumNumbers + double.Parse(item);
  24.             }
  25.            
  26.             Console.WriteLine(sumNumbers);
  27.         }
  28.         catch (System.FormatException)
  29.         {
  30.             Console.WriteLine("The numbers are wrong formated!");
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement