C program to generate the Fibonacci triangle
The Fibonacci triangle is a variation of the traditional Fibonacci sequence, in which each number is the sum of the two numbers before it. Each row in the Fibonacci triangle symbolizes a different Fibonacci sequence, and the numbers that make up each row are determined by the sum of the two numbers that come immediately above it. The rows stand in for the Fibonacci numbers, creating a triangle design.
The Fibonacci sequence is used to produce each row of a Fibonacci triangle, which is a pattern of numbers. The Fibonacci sequence is a set of integers where each number is the sum of the two before it, which is commonly begins with 0 and 1. The numbers are in this order: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
Let's examine the essential elements to comprehend the theory underlying the C program that creates the Fibonacci triangle:
1.Fibonacci Function (fibonacci):
The nth Fibonacci number is returned by the function known as the Fibonacci Function (fibonacci), which takes an integer n as input. It employs an iterative process to calculate Fibonacci numbers effectively. It calculates the nth Fibonacci number by adding the first two integers in the series, a and b, which are equal to 0 and 1, respectively. Up until the desired Fibonacci number (n), the loop iterates continuously. The result (b) is returned to the caller by the function.
2.Main Function:
The nu.mber of rows in the Fibonacci triangle is stored in the variable n, which is declared at the beginning of the main function. Using the scanf function, the user is prompted to input the desired number of rows for the triangle.
3.Generating the Fibonacci Triangle:
The Fibonacci Triangle is subsequently generated by the program, which enters a loop that iterates across each row of the triangle. A running inner loop prints the items of each row. The inner loop iterates from 0 to the current row number (i). The inner loop contains a call to the fibonacci function with the parameter i + j, where j ranges from 0 to i. With this, the Fibonacci numbers for the current row are generated.
For instance, i = 2 and the inner loop from j = 0 to j = 2 produce the numbers fibonacci (2+0), fibonacci(2+1), and fibonacci(2+2) when the third row's elements are to be printed.
4.Printing the Triangle:
The program prints the Fibonacci numbers in a triangle using stacked loops after generating them for each row. The inner loop iterates over the elements contained within each row while the outer loop iterates over the rows. A newline character is written to start the next line for the following row after publishing the entire previous row.
The program generates the corresponding Fibonacci triangle and displays it on the screen when it is run and the user inputs a value for n. The Fibonacci triangle's distinctive triangular pattern is formed by each number in the triangle being equal to the sum of the two numbers directly above it.
A Fibonacci triangle is a triangle where each integer in the row equals the sum of the two numbers above it.
Example:
Let's take a program that creates a Fibonacci triangle in C:
#include <stdio.h>
// Function to generate the nth Fibonacci number
int fibonacci(int n)
{
if (n <= 0) return 0;
if (n == 1) return 1;
int a = 0, b = 1, c;
for (int i = 2; i<= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
int main()
{
int n;
printf("Enter the number of rows for the Fibonacci triangle: ");
scanf("%d", &n);
for (int i = 0; i< n; i++)
{
for (int j = 0; j <= i; j++)
{
printf("%d ", fibonacci(i + j));
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows for the Fibonacci triangle: 5
0
1 1
1 2 3
2 3 5 8
3 5 8 13 21
Explanation:
The program asks the user to specify the number of rows they want in the triangle to generate a Fibonacci triangle. After that, the numbers in the triangle are generated using the fibonacci function, and the corresponding rows are printed.
For example, the Fibonacci triangle will be produced when the program is executed with a value of 5.