Advertisement
TodorovH

Sum of 5 Numbers

Mar 25th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. // Write a program that enters 5 numbers (given in a single line, separated by a space),
  5. // calculates and prints their sum.
  6.  
  7. class SumOf5Numbers
  8. {
  9. static void Main()
  10. {
  11. Console.WriteLine();
  12. string numbers = Console.ReadLine();
  13. string[] separatedNumbers = numbers.Split(new Char[] { ' ' });
  14. double sum = 0;
  15. foreach (string n in separatedNumbers)
  16. {
  17. if (n.Trim() != " ")
  18. {
  19. double numTrim = double.Parse(n);
  20. sum += numTrim;
  21. }
  22. }
  23. Console.WriteLine();
  24. Console.WriteLine(sum);
  25. Console.WriteLine();
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement