Sure! Here’s a C program that generates a Fibonacci Triangle:
#include <stdio.h> int fibonacci(int n) { if (n <= 1) return n; else return fibonacci(n - 1) + fibonacci(n - 2); } int main() { int rows, i, j, count = 0; printf("Enter the number of rows for the Fibonacci triangle: "); scanf("%d", &rows); printf("Fibonacci Triangle:\n"); for (i = 0; i < rows; i++) { for (j = 0; j <= i; j++) { printf("%d ", fibonacci(count)); count++; } printf("\n"); } return 0; }
This program prompts the user to enter the number of rows for the Fibonacci triangle. It then uses a nested loop to generate and print the Fibonacci numbers in a triangle pattern.
The fibonacci()
function is a recursive function that calculates the nth Fibonacci number. It takes an integer n
as input and returns the corresponding Fibonacci number.
The outer loop (i
) controls the number of rows in the triangle, and the inner loop (j
) prints the Fibonacci numbers for each row. The count
variable keeps track of the Fibonacci number to be printed.
When you run this program and enter the number of rows, it will generate and display the Fibonacci Triangle. Each number in the triangle represents a Fibonacci number.
Example output for rows = 5
:
Enter the number of rows for the Fibonacci triangle: 5 Fibonacci Triangle: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377