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

Q: How to find in C# - the sum of n numbers given?

+4 votes

Help me write a program that enters a number n and after that enters more n numbers (based on the first n number) and calculates and prints their sum. Maybe with a for-loop?

Examples:

numbers

sum

 

numbers

sum

 

numbers

sum

3

20

60

10

90

5

2

-1

-0.5

4

2

6.5

1

1

1

 

asked in C# category by user eiorgert
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Yes - indeed - the solution is with for loop.

See the 15th line - to view the view the sum of the numbers on one single line - the Console.WriteLine is OUTSIDE the for loop!

Here's mine:

using System;

class SumOfnNumbers
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        double sumOfNumbers = 0;
        for (int i = 0; i < n; i++)
        {
            double num = double.Parse(Console.ReadLine());
            sumOfNumbers += num;
        }
        Console.WriteLine(sumOfNumbers);
    }
}
answered by user nikole
selected by user golearnweb
...