Print Fibonacci triangle
Here given code implementation process.
//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
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