PSYCHOCODES

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\n");
	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\n";
	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

Tribonacci Series in C

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Share your thoughts