This C program prints out the first Fibonacci series (sequence) of N numbers. In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci. The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, thus creating the sequence 0, 1, 1, 2, 3, 5, 8, etc. The standard form of writing Fibonacci series is:
xn = xn-1 + xn-2
where:
- xn is term number “n”
- xn-1 is the previous term (n-1)
- xn-2 is the term before that (n-2)
So this program prints the N numbers of Fibonacci series in C on the screen where N is the integer number entered by the user. This C program prints maximum of 50 Fibonacci numbers.
This program uses the following C Programming topics so go thorough these articles for a better understanding of the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> int main(void) { //The number of Fibonacci numbers we will print int n; // The index of Fibonacci number to be printed next int i; // The value of the (i)th Fibonacci number long current; // The value of the (i+1)th Fibonacci number long next; // The value of the (i+2)th Fibonacci number long twoaway; printf("How many Fibonacci numbers do you want to compute? \n"); scanf("%d", &n); if (n<=0 || n>50 ) { printf("The number should be positive and less than 51.\n"); } else { printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n"); next = current = 1; for (i=1; i<=n; i++) { printf("\t%d \t %ld\n", i, current); twoaway = current+next; current = next; next = twoaway; } } } |
Output of the C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | How many Fibonacci numbers do you want to compute? 50 I Fibonacci(I) ===================== 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610 16 987 17 1597 18 2584 19 4181 20 6765 21 10946 22 17711 23 28657 24 46368 25 75025 26 121393 27 196418 28 317811 29 514229 30 832040 31 1346269 32 2178309 33 3524578 34 5702887 35 9227465 36 14930352 37 24157817 38 39088169 39 63245986 40 102334155 41 165580141 42 267914296 43 433494437 44 701408733 45 1134903170 46 1836311903 47 2971215073 48 4807526976 49 7778742049 50 12586269025 |