Index
Introduction – Fibonacci Sequence
Hello, here we are going to create an Fibonacci Sequence by the use of C Language. This is the basic level college project which is easily boost your skills with mathematical formula uses concepts.
Format of the Program Output
In this Program, First we generate the first “n” Fibonacci numbers. The user is prompted to enter the value of “n”, and the program then generates and displays the first “n” numbers in the Fibonacci Sequence.
Fibonacci formula
The Fibonacci Sequence is defined by the recurrence relation:
with the initial conditions:
This means each number is the sequence is the sum of the two preceding ones, starting from 0 to 1.
Project Codes – Fibonacci Sequence
Here is the complete Project codes:
#include <stdio.h> void generateFibonacci(int n) { long long int fib1 = 0, fib2 = 1, fibNext; printf("The first %d Fibonacci numbers are:\n", n); for (int i = 1; i <= n; ++i) { if (i == 1) { printf("%lld ", fib1); continue; } if (i == 2) { printf("%lld ", fib2); continue; } fibNext = fib1 + fib2; fib1 = fib2; fib2 = fibNext; printf("%lld ", fibNext); } printf("\n"); } int main() { int n; // Input the number of Fibonacci numbers to generate printf("Enter the number of Fibonacci numbers to generate: "); scanf("%d", &n); // Ensure the number is positive if (n <= 0) { printf("Please enter a positive integer.\n"); } else { generateFibonacci(n); } return 0; }