Find the maximum term of Fibonacci sequence whose values do
not exceed four million, also find the sum of the odd-valued terms.
Hints:
The Fibonacci Sequence is the series
of numbers:
0,
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding
up the two numbers before it.
- The 2 is found by adding the two numbers before it (1+1)
- Similarly, the 3 is found by adding the two numbers before it (1+2)
- And the 5 is (2+3),
- and so on!
Example: the next number in the
sequence above would be 21+34 = 55
So we can write the rule:
The Rule is xn = xn-1
+ xn-2
where:
- xn is term number "n"
- xn-1 is the previous term (n-1)
- xn-2 is the term before that (n-2)
#include<stdio.h>
int main( )
{
long sum=0,first=0,second=1,final=0;
printf ("\n%d",first);
printf ("\n%d",second);
while (second < = 4000000)
{
final = first + second;
if (second % 2 != 0)
{
sum += second;
}
first = second;
second = final;
}
printf ("\nThe value of maximum term is: %d",final);
printf ("\nThe sum of odd Fibonace sequence is: %d ",sum);
return 0;
}
Link of C programming Index/Sub Page
int main( )
{
long sum=0,first=0,second=1,final=0;
printf ("\n%d",first);
printf ("\n%d",second);
while (second < = 4000000)
{
final = first + second;
if (second % 2 != 0)
{
sum += second;
}
first = second;
second = final;
}
printf ("\nThe value of maximum term is: %d",final);
printf ("\nThe sum of odd Fibonace sequence is: %d ",sum);
return 0;
}
Link of C programming Index/Sub Page
#include
উত্তরমুছুন#include
int main()
{
long sum=0,first=0,second=1,final=0;
printf("\n%d",first);
printf("\n%d",second);
while (second <=4000000)
{
final = first + second;
if (second % 2 != 0)
sum += second;
first = second;
second = final;
printf("\n%d",final);
}
printf("\n");
printf("The sum of odd Fibonace sequence is:%d",sum);
return 0;
}
#include
উত্তরমুছুনusing namespace std;
int fibonacci_number(int a=0,int b=1)
{
int c=0;
cout<<" "<<a<<endl;
cout<<" "<<b<<endl;
while(b<=4000000)
{
c=a+b;
a=b;
b=c;
cout<<" "<<c<<endl;
}
}
int odd()
{
int a=0,b=1,c=0,sum=0;
if(b % 2!=0)
sum+=b;
}
main()
{
int sum;
fibonacci_number();
odd();
cout<<"the sum of odd fibonacci sequence is:"<<sum;
return 0;
}