The Fibonacci sequence is constructed by adding the last two numbers of the sequence so far to get the next number in the sequence. The first and the second numbers of the sequence are defined as 0 and 1. We get:
0, 1, 1, 2, 3, 5, 8, 13, 21…
Write a function which takes input as a number:
If the given number is a Fibonacci number, print the number
If the given number is NOT a Fibonacci number, print the sum of all odd Fibonacci numbers less than the given number.
using namespace std;
/*
The Fibonacci sequence is constructed by adding the last two numbers of the sequence so far to get the next number in the sequence.
The first and the second numbers of the sequence are defined as 0 and 1. We get:
0, 1, 1, 2, 3, 5, 8, 13, 21…
Write a funusing namespace std;
/*
The Fibonacci sequence is constructed by adding the last two numbers of the sequence so far to get the next number in the sequence.
The first and the second numbers of the sequence are defined as 0 and 1. We get:
0, 1, 1, 2, 3, 5, 8, 13, 21…
Write a function which takes input as a number:
if the given number is a Fibonacci number, print the number
If the given number is NOT a Fibonacci number, print the sum of all odd Fibonacci numbers less than the given number.
*/
int main()
{
int n,a,b,c,u,Sum=0;
a=0; //First Fibonacci number
b=1; //Second Fibonacci number
cout<<"\n\tEnter the number: "; cin>>n;
if(n==0)
{
cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacii elements < "<<n<<" is: "<<Sum;
}
if(n==1)
{
Sum=1;
cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacii elements < "<<n<<" is: "<<Sum;
}
c=0;
Sum=0;
while(c<=n)
{
c=a+b;
if(c%2==1) Sum=Sum+c;
a=b;
b=c;
if(c==n) cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
}
if(c!=n) cout<<"\n\tThe input number "<<n<<" is NOT a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacci elements < "<<n<<" = "<<Sum;
return(0);
}ction which takes input as a number:
if the given number is a Fibonacci number, print the number
If the given number is NOT a Fibonacci number, print the sum of all odd Fibonacci numbers less than the given number.
*/
int main()
{
int n,a,b,c,u,Sum=0;
a=0; //First Fibonacci number
b=1; //Second Fibonacci number
cout<<"\n\tEnter the number: "; cin>>n;
if(n==0)
{
cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacii elements < "<<n<<" is: "<<Sum;
}
if(n==1)
{
Sum=1;
cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacii elements < "<<n<<" is: "<<Sum;
}
c=0;
Sum=0;
while(c<=n)
{
c=a+b;
if(c%2==1) Sum=Sum+c;
a=b;
b=c;
if(c==n) cout<<"\n\tThe input number "<<n<<" is a Fibonacci Number.";
}
if(c!=n) cout<<"\n\tThe input number "<<n<<" is NOT a Fibonacci Number.";
cout<<"\n\tSum of all odd fibonacci elements < "<<n<<" = "<<Sum;
return(0);
}
Comments
Leave a comment