pupesko

SoftUni - 4.7* Sum of 5 Numbers

Oct 1st, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. //Write a program that enters 5 numbers (given in a single line, separated by a space), calculates and prints their sum.
  2.  
  3. using System;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Sum5Numbers
  7. {
  8.     static void Main()
  9.     {
  10.         Console.Write("Enter Five numbers with spaces:");
  11.         string numFive = Console.ReadLine();
  12.  
  13.         numFive = Regex.Replace(numFive, @"\s+", " ");
  14.  
  15.         string[] numbers = numFive.Split(' ');
  16.         double sum = 0.0;
  17.  
  18.         foreach (string number in numbers)
  19.         {
  20.             double newNum = double.Parse(number);
  21.             sum = sum + newNum;
  22.         }
  23.  
  24.         Console.WriteLine("The sum of Five numbers is: " + sum);
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment