Print Fibonacci triangle
In this article, we will discuss how to print a Fibonacci triangle using a C program. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. A Fibonacci triangle is a triangular arrangement of the Fibonacci numbers.
Problem Statement
We are given the number of rows in the Fibonacci triangle, and we need to print the triangle with the corresponding Fibonacci numbers.
Example
Let's consider an example to understand the problem better. If we want to print a Fibonacci triangle with 7 rows, the output will be as follows:
ROW SIZE: 7 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811
Approach
To solve this problem, we will use a nested loop structure. The outer loop will iterate through the rows, and the inner loop will print the Fibonacci numbers for each row.
Algorithm
- Start with two initial values: first = 1 and second = 0.
- Initialize two variables, i and j, to keep track of the row and column.
- For each row from 0 to the given number of rows:
- For each column from 0 to the current row:
- Print the current Fibonacci number (first).
- Calculate the next Fibonacci number:
- first = second + first
- second = first - second
- Move to the next line to print the next row.
Pseudocode
function fibonacci_triangle(row)
first = 1
second = 0
for i = 0 to row
for j = 0 to i
print first
first = second + first
second = first - second
print new line
Code Solution
//C Program
//Display Fibonacci triangle
#include <stdio.h>
//Print Fibonacci triangle of given rows
void fibonacci_triangle(int row)
{
printf("ROW SIZE : %d\n\n", row);
//This is two initial value
int first = 1;
int second = 0;
int j = 0;
int i = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j <= i; j++)
{
printf("%d\t", first);
//Get next fibonacci number
first = second + first;
second = first - second;
}
printf("\n");
}
printf("\n\n");
}
int main()
{
//Test Case
fibonacci_triangle(7);
fibonacci_triangle(5);
return 0;
}
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// Java Program
// Display Fibonacci triangle
class MyPattern
{
//Print Fibonacci triangle of given rows
public void fibonacci_triangle(int row)
{
System.out.print("ROW SIZE : " + row + "\n\n");
//This is two initial value
int first = 1;
int second = 0;
int j = 0;
int i = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j <= i; j++)
{
System.out.print("" + first + "\t");
//Get next fibonacci number
first = second + first;
second = first - second;
}
System.out.print("\n");
}
System.out.print("\n\n");
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
}
}
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// C++ Program
// Display Fibonacci triangle
#include<iostream>
using namespace std;
class MyPattern
{
public:
//Print Fibonacci triangle of given rows
void fibonacci_triangle(int row)
{
cout << "ROW SIZE : " << row << "\n\n";
//This is two initial value
int first = 1;
int second = 0;
int j = 0;
int i = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j <= i; j++)
{
cout << "" << first << "\t";
//Get next fibonacci number
first = second + first;
second = first - second;
}
cout << "\n";
}
cout << "\n\n";
}
};
int main()
{
MyPattern obj = MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
return 0;
}
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
<?php
// Php Program
// Display Fibonacci triangle
class MyPattern
{
//Print Fibonacci triangle of given rows
public function fibonacci_triangle($row)
{
echo("ROW SIZE : ". $row ."\n\n");
//This is two initial value
$first = 1;
$second = 0;
$j = 0;
$i = 0;
for ($i = 0; $i < $row; ++$i)
{
for ($j = 0; $j <= $i; $j++)
{
echo("". $first ."\t");
//Get next fibonacci number
$first = $second + $first;
$second = $first - $second;
}
echo("\n");
}
echo("\n\n");
}
}
function main()
{
$obj = new MyPattern();
//Test Cases
$obj->fibonacci_triangle(7);
$obj->fibonacci_triangle(5);
}
main();
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// Node Js Program
// Display Fibonacci triangle
class MyPattern
{
//Print Fibonacci triangle of given rows
fibonacci_triangle(row)
{
process.stdout.write("ROW SIZE : " + row + "\n\n");
//This is two initial value
var first = 1;
var second = 0;
var j = 0;
var i = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j <= i; j++)
{
process.stdout.write("" + first + "\t");
//Get next fibonacci number
first = second + first;
second = first - second;
}
process.stdout.write("\n");
}
process.stdout.write("\n\n");
}
}
function main(args)
{
var obj = new MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
}
main();
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
# Python 3 Program
# Display Fibonacci triangle
class MyPattern :
# Print Fibonacci triangle of given rows
def fibonacci_triangle(self, row) :
print("ROW SIZE : ", row ,"\n")
# This is two initial value
first = 1
second = 0
j = 0
i = 0
i = 0
while (i < row) :
j = 0
while (j <= i) :
print(first , end = "\t")
# Get next fibonacci number
first = second + first
second = first - second
j += 1
print( end = "\n")
i += 1
print("\n")
def main() :
obj = MyPattern()
# Test Cases
obj.fibonacci_triangle(7)
obj.fibonacci_triangle(5)
if __name__ == "__main__": main()
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
# Ruby Program
# Display Fibonacci triangle
class MyPattern
# Print Fibonacci triangle of given rows
def fibonacci_triangle(row)
print("ROW SIZE :", row ,"\n\n")
# This is two initial value
first = 1
second = 0
j = 0
i = 0
i = 0
while (i < row)
j = 0
while (j <= i)
print("", first ,"\t")
# Get next fibonacci number
first = second + first
second = first - second
j += 1
end
print("\n")
i += 1
end
print("\n\n")
end
end
def main()
obj = MyPattern.new()
# Test Cases
obj.fibonacci_triangle(7)
obj.fibonacci_triangle(5)
end
main()
Output
ROW SIZE :7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE :5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// Scala Program
// Display Fibonacci triangle
class MyPattern
{
//Print Fibonacci triangle of given rows
def fibonacci_triangle(row: Int): Unit = {
print("ROW SIZE : " + row + "\n\n");
//This is two initial value
var first: Int = 1;
var second: Int = 0;
var j: Int = 0;
var i: Int = 0;
i = 0;
while (i < row)
{
j = 0;
while (j <= i)
{
print("" + first + "\t");
//Get next fibonacci number
first = second + first;
second = first - second;
j += 1;
}
print("\n");
i += 1;
}
print("\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
}
}
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// Swift Program
// Display Fibonacci triangle
class MyPattern
{
//Print Fibonacci triangle of given rows
func fibonacci_triangle(_ row: Int)
{
print("ROW SIZE : ", row ,"\n");
//This is two initial value
var first: Int = 1;
var second: Int = 0;
var j: Int = 0;
var i: Int = 0;
i = 0;
while (i < row)
{
j = 0;
while (j <= i)
{
print(first ,"\t", terminator: "");
//Get next fibonacci number
first = second + first;
second = first - second;
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n\n", terminator: "");
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
}
main();
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
// C# Program
// Display Fibonacci triangle
using System;
class MyPattern
{
//Print Fibonacci triangle of given rows
public void fibonacci_triangle(int row)
{
Console.Write("ROW SIZE : " + row + "\n\n");
//This is two initial value
int first = 1;
int second = 0;
int j = 0;
int i = 0;
i = 0;
while (i < row)
{
j = 0;
while (j <= i)
{
Console.Write("" + first + "\t");
//Get next fibonacci number
first = second + first;
second = first - second;
j++;
}
Console.Write("\n");
i++;
}
Console.Write("\n\n");
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.fibonacci_triangle(7);
obj.fibonacci_triangle(5);
}
}
Output
ROW SIZE : 7
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811
ROW SIZE : 5
1
1 2
3 5 8
13 21 34 55
89 144 233 377 610
Time Complexity
The time complexity of this solution is O(n^2), where n is the number of rows in the Fibonacci triangle. This is because we have nested loops that iterate up to n.
Finally
In this article, we learned how to print a Fibonacci triangle using a C program. We discussed the problem statement, provided an example, explained the algorithm and pseudocode, and analyzed the time complexity of the solution. The C code implementation and its corresponding output were also presented.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment