@lmdat/

bai-10

C#

Tính tổng S(n) = 1 + 2 +3 + ... + n

fork
loading
Files
  • main.cs

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.44303047952566943","path":"main.cs","file":{"path":"main.cs","content":{"asEncoding":{"base64":"dXNpbmcgU3lzdGVtOwoKY2xhc3MgUHJvZ3JhbSB7CiAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4gKHN0cmluZ1tdIGFyZ3MpIHsKICAgIC8vIEto4bufaSB04bqhbyBiaeG6v24gbgogICAgaW50IG4gPSAwOwoKICAgIC8qCiAgICAgKiBEw7luZyB2w7JuZyBs4bq3cCBkby13aGlsZSDEkeG7gyBraeG7g20gdHJhIAogICAgICogZ2nDoSB0cuG7iyBuaOG6rXAgdOG7qyBiw6BuIHBow61tIAogICAgICogY8OzIHBo4bqjaSBsw6Agc+G7kSBuZ3V5w6puIGTGsMahbmcgaGF5IGtow7RuZy4KICAgICAqLwogICAgZG8KICAgIHsKICAgICAgQ29uc29sZS5Xcml0ZSgiTmhhcCBzbyBuZ3V5ZW4gZHVvbmcgbjogIik7CiAgICAgIG4gPSBDb252ZXJ0LlRvSW50MzIoQ29uc29sZS5SZWFkTGluZSgpKTsKICAgIH0gd2hpbGUgKG4gPCAxKTsKCiAgICAvL0to4bufaSB04bqhbyBiaeG6v24gc3VtCiAgICB1aW50IHN1bSA9IDA7CgogICAgZm9yICh1aW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgIHsKICAgICAgICBzdW0gKz0gaTsgLy8gVMawxqFuZyDEkcawxqFuZzogc3VtID0gc3VtICsgaTsKICAgIH0KCiAgICBDb25zb2xlLldyaXRlTGluZSgiUyh7MH0pID0gezF9Iiwgbiwgc3VtKTsKICAgIC8vIEThu6tuZyBtw6BuIGjDrG5oIMSR4buDIHhlbSBr4bq/dCBxdeG6owogICAgQ29uc29sZS5SZWFkTGluZSgpOwogIH0KfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;

class Program {
  public static void Main (string[] args) {
    // Khởi tạo biến n
    int n = 0;

    /*
     * Dùng vòng lặp do-while để kiểm tra 
     * giá trị nhập từ bàn phím 
     * có phải là số nguyên dương hay không.
     */
    do
    {
      Console.Write("Nhap so nguyen duong n: ");
      n = Convert.ToInt32(Console.ReadLine());
    } while (n < 1);

    //Khởi tạo biến sum
    uint sum = 0;

    for (uint i = 1; i <= n; i++)
    {
        sum += i; // Tương đương: sum = sum + i;
    }

    Console.WriteLine("S({0}) = {1}", n, sum);
    // Dừng màn hình để xem kết quả
    Console.ReadLine();
  }
}