Learn how to print Tribonacci series in C programming language. The Tribonacci sequence is very similar to Fibonacci sequence.
What is a Tribonacci Sequence?
A Tribonacci sequence is a sequence of numbers in which the next number is found by adding the previous three consecutive numbers.
Note: The first three digits in a Tribonacci series is always 0, 1 and 2.
A Tribonacci sequence consists of first digit as 0, second digit as 1 and third digit as 2. The next digit (fourth element) is dependent upon the preceding three elements.
The fourth element is, therefore, the sum of previous three digits. This addition of previous three digits continues till the limit.
So, primarily Fibonacci series makes use of two preceding digits whereas tribonacci sequences makes use of preceding three digits
Example
0 1 2 3 6 11 20 37 68
Method 1: Print Tribonacci Series in C Programming using For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> int main() { int num, x = 0, y = 1, z = 2, new_variable = 0, count; printf("Enter A Number:\t"); scanf("%d", &num); printf("%d\t%d\t%d\t", x, y, z); for(count = 3; count < num; count++) { new_variable = x + y + z; printf("%d\t", new_variable); x = y; y = z; z = new_variable; } return 0; } |
Method 2: C Program To Generate Tribonacci Sequence using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { int num, x = 0, y = 1, z = 2, new_variable = 0, count; printf("Enter A Number:\t"); scanf("%d", &num); printf("%d\t%d\t%d\t", x, y, z); count = 3; while(count < num) { new_variable = x + y + z; printf("%d\t", new_variable); x = y; y = z; z = new_variable; count = count + 1; } return 0; } |
Method 3: C Program To Print Tribonacci Sequence using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<stdio.h> void tribonacci(int x, int y, int z, int new_variable, int limit) { for(int count = 0; count < limit; count++) { new_variable = x + y + z; printf("%d\t", new_variable); x = y; y = z; z = new_variable; } } int main() { int limit, first = 0, second = 1, third = 2, new_variable = 0; printf("\nEnter Total Number of Elements:\t"); scanf("%d", &limit); printf("\n%d\t%d\t%d\t", first, second, third); tribonacci(first, second, third, new_variable, limit); return 0; } |
Output

In case you get any compilation errors in the above code to print Tribonacci series in C programming using For loop and While loop or if you have any doubts about it, let us know about it in the comment section below.
CodeLite is an amazing IDE. Thank you so much for getting it here. It’s better than Codeblocks.
Hey. This Tribonacci sequence seems to be so easy due to such a simple code. Thanks.