settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to calculate 5 numbers - each on one line in C#?

+3 votes

Please help me find a way to calculate 5 numbers - each on one line (NOT ON A DIFFERENT) in C#. I am a newbie - and I am confused with this task! Thanks

asked in C# category by user andrew

1 Answer

+1 vote
 
Best answer

You must put them in an array - and since you know how many elements you will have in the array - the calculation should be easy! You should only parse the numerical value.

Here it is:

using System;

class SumOfFiveNumbers
{
    static void Main()
    {
        string[] numbers = Console.ReadLine().Split(' ');
        decimal a = decimal.Parse(numbers[0]);
        decimal b = decimal.Parse(numbers[1]);
        decimal c = decimal.Parse(numbers[2]);
        decimal d = decimal.Parse(numbers[3]);
        decimal e = decimal.Parse(numbers[4]);
        
        Console.WriteLine(a+b+c+d+e);
    }
}

 

answered by user eiorgert
selected by user golearnweb
...