@nakov/

for-loop-sum-n-numbers-csharp

C#

Sum n integers in C# using loops

fork
loading
Files
  • main.cs
  • main.exe

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7416489586130712","path":"main.cs","file":{"path":"main.cs","content":{"asEncoding":{"base64":"dXNpbmcgU3lzdGVtOwoKY2xhc3MgU3VtTnVtYmVyc1dpdGhGb3JMb29wIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkgewogICAgICAgIGludCBuID0gaW50LlBhcnNlKENvbnNvbGUuUmVhZExpbmUoKSk7CiAgICAgICAgbG9uZyBzdW0gPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IG51bSA9IGludC5QYXJzZShDb25zb2xlLlJlYWRMaW5lKCkpOwogICAgICAgICAgICBzdW0gKz0gbnVtOwogICAgICAgIH0KICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiU3VtID0gezB9Iiwgc3VtKTsKICAgIH0KfQo="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;

class SumNumbersWithForLoop {
    public static void Main() {
        int n = int.Parse(Console.ReadLine());
        long sum = 0;
        for (int i = 0; i < n; i++)
        {
            int num = int.Parse(Console.ReadLine());
            sum += num;
        }
        Console.WriteLine("Sum = {0}", sum);
    }
}