# Tribonacci Series Program in C and C++

What is Tribonacci Series?

Tribonacci Series is the Successor of Fibonacci Series in which each element is the sum of the previous three consecutive elements.

Note: The first three elements of the Tribonacci Series will be always 0, 1 and 2.

The first three elements of the tribonacci series remain independent as from the fourth element the rule for tribonacci series is applied.

Example of Tribonacci Series

0 1 2 3 6 11 20 37

Let we consider the number 20 from the above series, now if we add it’s previous three element 3+6+11 we get the answer 20.

Now let’s create a C program to print Tribonacci Series up to n elements where n will be taken from the user.

C program for Tribonacci Series

``#include<stdio.h>  int main(){ int n,i; int a=0,b=1,c=2,d; printf("Enter a number"); scanf("\%d",&n); printf("\%d \%d \%d ",a,b,c); for(i=0;i<n-3;i++){ d = a + b + c; printf("\%d ",d); a = b; b = c; c = d;  } }``

C++ program for Tribonacci Series

``#include<iostream>  using namespace std;  int main(){ int n,i; int a=0,b=1,c=2,d; cout<<"Enter a number"; cin>>n; cout<<a<<" "<<b<<" "<<c<<" "; for(i=0;i<n-3;i++){ d = a + b + c; cout<<d<<" "; a = b; b = c; c = d;  } return 0; }``

Output of the above programs Also, don’t forget to subscribe to our Newsletter.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: