Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created tribonacci_number.cpp #263

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CPP/tribonacci_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Problem Statement : The Tribonacci sequence Tn is defined as follows:
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Given n, return the value of Tn.
*/

#include<bits/stdc++.h>
using namespace std;

// Function that returns the nth tribonacci number

int tribonacci(int n) {
if (n < 3) return (n != 0);
int dp[n + 1];
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
} return dp[n];
}

// Main function

int main() {

int n;
cout << "Enter the number n : " << "\n";
cin >> n;
cout << "The value of Tn is : " << tribonacci(n);

return 0;
}