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 1, 8, 54, 384…

Giới thiệu

Cho số nguyên N. Tìm số hạng thứ N của dãy: 1, 8, 54, 384…

Ví dụ minh họa

Đầu vào

N = 4

Đầu ra

384

Giải thích

4th term = 4 * 4 * (4!) = 16 * 24 = 384

Phân tích dãy

Công thức chung: T(N) = N × N × N!

Chương trình minh họa

#include <iostream>
using namespace std;

int calcFact(int N) {
    int fact = 1;
    for (int i = 1; i <= N; i++)
        fact *= i;
    return fact;
}

int calcNthTerm(int N) {
    return N * N * calcFact(N);
}

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

Đầu ra

5th term of the series is 3000