Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

หาพจน์ที่ N ของอนุกรม 0, 11, 28, 51, 79, 115, 156, 203,... ใน C++

หาพจน์ที่ N ของอนุกรม: 0, 11, 28, 51, 79, 115, 156, 203, ...

ตัวอย่าง: N = 5 → 79

วิธีแก้ปัญหา (Quadratic Formula)

สูตรทั่วไปของพจน์ที่ n:

Tn = 3×N² + 2×N - 5

ตัวอย่างโค้ด C++

#include <iostream>
using namespace std;

int findNTerm(int N) {
    int nthTerm = (3 * N * N) + 2 * N - 5;
    return nthTerm;
}

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

Output

9th term of the series is 256