This C program prints out the first Fibonacci series 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.

So this program prints the N numbers of Fibonacci series on the screen where N is the integer number enterd by the user.

[code=’c’]
/*******************************************************
* MYCPLUS Sample Code – http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include

int main(void) {
int n; /* The number of fibonacci numbers we will print */
int i; /* The index of fibonacci number to be printed next */
int current; /* The value of the (i)th fibonacci number */
int next; /* The value of the (i+1)th fibonacci number */
int twoaway; /* The value of the (i+2)th fibonacci number */

printf(“How many Fibonacci numbers do you want to compute? “);
scanf(“%d”, &n);
if (n<=0) printf("The number should be positive.n"); else { printf("nntI t Fibonacci(I) nt=====================n"); next = current = 1; for (i=1; i<=n; i++) { printf("t%d t %dn", i, current); twoaway = current+next; current = next; next = twoaway; } } } /* The output from a run of this program was: How many Fibonacci numbers do you want to compute? 9 I Fibonacci(I) ===================== 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 */[/code]

0 Shares
Share
+1
Tweet
Share
Stumble