Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chương trình C++ tìm số hạng thứ N của dãy 2, 10, 30, 68, 130…

Giới thiệu

Cho số nguyên N. Tìm số hạng thứ N của dãy: 2, 10, 30, 68, 130…

Ví dụ minh họa

Đầu vào

N = 7

Đầu ra

350

Giải thích

Dãy: 2, 10, 30, 68, 130, 222, 350...

Phân tích dãy

Quan sát: T(1)=1³+1, T(2)=2³+2, T(3)=3³+3... Công thức chung: T(N) = N³ + N

Chương trình minh họa

#include <iostream>
using namespace std;

int findNthTerm(int N) {
    return (N*N*N) + N;
}

int main() {
    int N = 8;
    cout << "The " << N << "th term of the series is " << findNthTerm(N);
    return 0;
}

Đầu ra

The 8th term of the series is 520