In this example you will learn how to add two or more numbers together on the same line c# console app
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Program { static void Main(string[] args) { //adding two numbers together on the same line c# console app string line; int total = 0; Console.WriteLine("Enter numbers by leaving a space between them"); line = Console.ReadLine(); foreach (var item in line.Split(' ') ) { total += int.Parse(item); } Console.WriteLine("Sum of numbers is :"+total); Console.ReadLine(); } } |
Output: