Sunday, January 11, 2015

C Program That Count Odd Number In Fibonacci Series

Hello friends today i am sharing a simple program that will count odd numbers with in Fibonacci series.But before that you have know about fibonacci series,so let me explain.

What is Fibonacci Series ?
       The Fibonacci series 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.
In mathematical terms,the sequence F(n) of Fibonacci number is defined by following recurrence relation.
F(n)=F(n-1) + F(n-2) 
with 
F(1)=1 ,F(2)=1
Or 
F(0)=0,F(1)=1

For example:-

1,1,2,3,5,8,13,21,34,........
or
0,1,1,2,3,5,8,13,21,34,.........


                         So if you enter 8, then till 8 we have 4 odd numbers(1,1,3,5).Similarly if you enter 34,then till 34 we have 6 odd numbers(1,1,3,5,13,21). 


Now try to understand this code,I am sure you can easily understand this code..:) :)


Code :-


#include<stdio.h>

#include<conio.h>

int main(){

int num,a=0,b=1,c,count=1;

printf("\n\n\n\n WAP to count odd numbers in fibonacci series");

printf("\n Enter the number:");

scanf("%d",&num);

while(1)

{

c=a+b;


if(c==num)

{

if(c%2!=0)

        { count++;

        }

break;

}

if(c<num)

{

              if(c%2!=0)

               {

                 count++;

               }

}

if(c>num)

{

break;

}

a=b;

b=c;

}

printf("Total odd numbers: %d",count);

getch();

return(0);

}



Output :-

www.learning-tutorials.blogspot.com




*****************Thanks Friends ****************











No comments:

Post a Comment

Please Give Me Your Views

Popular Posts