This is a C program to calculate Fibonacci numbers upto n number.
Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. ... F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
like here, sum of 0 and 1 equals to 1 also sum of 1 and 1 equals to 2 also sum of 1 and 2 equals to 3 and so on.
Source Code:
- #include <stdio.h>
- int main()
- {
- //Variable Declaration.
- int n, first = 0, second = 1, next, c;
- //Take Input.
- printf("Enter the number of terms\n");
- scanf("%d", &n);
- printf("First %d terms of Fibonacci series are:\n", n);
- //Working Formula
- for (c = 0; c < n; c++)
- {
- if (c <= 1)
- next = c;
- else
- {
- next = first + second;
- first = second;
- second = next;
- }
- printf("%d\t", next);
- }
- return 0;
- }
Output:
Enter the number of terms
10
First 10 terms of Fibonacci series are:
0 1 1 2 3 5 8 13 21 34
Enter the number of terms
20
First 20 terms of Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
610 987 1597 2584 4181
0 comments:
Post a Comment