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

Tìm số hạng thứ N của dãy 3, 6, 18, 24... trong C++

Cho số N. Tìm số hạng thứ N của dãy: 3, 6, 18, 24, 45, 54, 84, ...

Công thức chung

Tn = n × (n/2 + (n%2)×2 + n) [Lưu ý: công thức gốc có thể cần kiểm tra lại]

Ví dụ

N = 10 → 150

N = 7 → 84

Ví dụ minh họa (C++)

#include <iostream>
using namespace std;

int findNTerm(int N) {
    int nthTerm = N * ((N / 2) + ((N % 2) * 2) + N);
    return nthTerm;
}

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

Đầu ra

7th term of the series is 84