Sunday, 8 July 2012

Program to print Fibonacci series in C


Code -



/* ___ Program to print the Fibonnaci series _____ */

/* Program will take a number as an input and print that much numbers of the fibonacci series */

/* Logic - Fibonacci series starts from 0 and the second number is 1. The third number can be

calculated by adding the previous two numbers */

#include<stdio.h>

#include<conio.h>

int main()
{
int first_number = 0, second_number = 0, third_number = 0, series_length = 0, temp = 0;

printf("\n\t ______ Program to print Fibonacci Series ______");

printf("\n\n  Enter the Length of the Series - ");

scanf("%d", &series_length);

//__ Prompting the user to input the total numbers to be printed in the fibonacci series ____

first_number = 0;

second_number = 1;

printf("\n\n  Fibonacci Series -\n\n  \t%d, %d", first_number, second_number);

for(temp = 3; temp <= series_length; temp++)
{
third_number = first_number + second_number;

printf(", %d", third_number);

//______ Assigning new value to the variables first_number and second_number _______

first_number = second_number;

second_number = third_number;

}

getch(); //____ Holding the Output ____

return 0;

/*______ Return 0 tells the compiler that program has executed successfully without any

error_______*/


}

No comments:

Post a Comment