Share 1*2+2*3+3*4+...+n(n+1) c++

** 1.1 \*2+2 \*3+3 \*4+\ ...+n (n+1) trong C ++ **

Mã sau đây tính toán tổng của các số tự nhiên `n` đầu tiên bằng công thức` 1 \*2+2 \*3+3 \*4+\ ...+n (n+1) `trong c ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

int main () {
// Nhận số lượng tự nhiên để tổng.
int n;
cin >> n;

// Tính tổng của n số tự nhiên đầu tiên.
int sum = 0;
for (int i = 1; i <= n; i ++) {
sum + = i * (i + 1);
}

// In tổng.
cout << sum << endl;

trả lại 0;
}
`` `

** 2.Đầu ra ví dụ **

`` `
Nhập số số tự nhiên vào tổng: 5
15
`` `

** 3.Giải trình**

Dòng đầu tiên của mã bao gồm tệp tiêu đề `<iostream>`, cung cấp các đối tượng `cin` và` cout` cho đầu vào và đầu ra, tương ứng.

Dòng thứ hai xác định câu lệnh `sử dụng không gian tên std`, cho phép chúng tôi sử dụng các đối tượng` cin` và `cout` mà không phải tiền tố chúng với không gian tên` std :: `.

Dòng thứ ba là hàm `main ()`, là điểm nhập cho tất cả các chương trình C ++.

Dòng thứ tư nhận được số lượng tự nhiên để tổng từ người dùng.

Dòng thứ năm tính toán tổng của n số tự nhiên đầu tiên bằng công thức `1 \*2+2 \*3+3 \*4+\ ...+n (n+1).

Dòng thứ sáu in tổng vào bảng điều khiển.

Dòng thứ bảy trả về 0 để chỉ ra rằng chương trình đã hoàn thành thành công.

**4.Người giới thiệu**

* [Hướng dẫn C ++: cho Loop] (C++ for loop)
* [Hướng dẫn C ++: toán tử số học] (https://www.tutorialspoint.com/cplusplus/cpp_arithmetic_operators.htm)
* [Hướng dẫn C ++: Đầu vào và đầu ra] (https://www.tutorialspoint.com/cplusplus/cpp_input_output.htm)

** 5.Hashtags **

* #C ++
* #Programming
* #algorithms
* #Toán học
* #tutorial
=======================================
**1. 1\*2+2\*3+3\*4+\...+N(n+1) in C++**

The following code calculates the sum of the first `N` natural numbers using the formula `1\*2+2\*3+3\*4+\...+N(n+1)` in C++:

```c++
#include <iostream>

using namespace std;

int main() {
// Get the number of natural numbers to sum.
int N;
cin >> N;

// Calculate the sum of the first N natural numbers.
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i * (i + 1);
}

// Print the sum.
cout << sum << endl;

return 0;
}
```

**2. Example output**

```
Enter the number of natural numbers to sum: 5
15
```

**3. Explanation**

The first line of the code includes the `<iostream>` header file, which provides the `cin` and `cout` objects for input and output, respectively.

The second line defines the `using namespace std` statement, which allows us to use the `cin` and `cout` objects without having to prefix them with the `std::` namespace.

The third line is the `main()` function, which is the entry point for all C++ programs.

The fourth line gets the number of natural numbers to sum from the user.

The fifth line calculates the sum of the first N natural numbers using the formula `1\*2+2\*3+3\*4+\...+N(n+1)`.

The sixth line prints the sum to the console.

The seventh line returns 0 to indicate that the program has successfully completed.

**4. References**

* [C++ Tutorial: For Loop](https://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm)
* [C++ Tutorial: Arithmetic Operators](https://www.tutorialspoint.com/cplusplus/cpp_arithmetic_operators.htm)
* [C++ Tutorial: Input and Output](https://www.tutorialspoint.com/cplusplus/cpp_input_output.htm)

**5. Hashtags**

* #C++
* #Programming
* #algorithms
* #Math
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top