C Program to Print Out Fibonacci Series upto N Terms


Really for coding one also must learn the Mathematics. There are many programs out there which are totally based on mathematics questions. And I hate maths 😦 Love to coding only 😉

Well Let Face It. Lets make a program to print out Fibonacci series up to N terms. A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers.. For example we have to print up to 5 then the series will be 0,1,1,2,3, here every number is the sum of last two numbers.

Lets Have ah Look at the Program  ►►

#include<stdio.h>

#include<conio.h>

void main()

{

int next,first=0,second=1,n;

printf(“Enter the number of Terms\n”);

scanf(“%d”,&n);

printf(“First %d terms of Fibonacci Series are : \n”);

for(int c=0;c<n;c++)

{

        if(c<=1)

        next=c;

        else

        {

        next=first+second;

        first=second;

        second=next;

        }

        printf(“%d\t”,next);

}//for close brace

getch();

}

Run it and find the desired Fibonacci Series..

Explanation : What I did is first take the number of terms from keyboard to print the Fibonacci Series up to that number. Then conditioned the for loop to calculate series while c<n. And if c< or = 1 then next=c; as when the number is 0, the first and second number will be 0, that means the term will be 0 and then for c=1, 1+0 = 1; means for c=0 and c=1 terms will be the same. And then in the else part I state next=first+second; as on initialization first=0 and second=1, so output will be 1 as next will be 1, then first=second; means now first will be 1 and second is equal to next which will also 1 that will output 2 as the sum of boh. And so on.

I done it for all numbers up to n and print them out at the same time.

Advertisements

2 thoughts on “C Program to Print Out Fibonacci Series upto N Terms”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s