Write a program that inputs a number form the user and display Fibonacci series upto number entered.
Solution :
#include <iostream.h>
#include <conio.h>
int main ()
{
int a, b, next, n;
clrscr();
cout<< "Enter the number upto which fibonacci series is required: " ;
cin>>n;
cout<< "Fibonacci series upto "<< n<< "is " << endl;
a = 0 ;
b = 1 ;
cout<< a<< " "<< b;
next = a + b;
while (next <= n)
{
cout << " "<< next;
a = b;
b = next;
next = a + b;
}
return 0;
}
#include <conio.h>
int main ()
{
int a, b, next, n;
clrscr();
cout<< "Enter the number upto which fibonacci series is required: " ;
cin>>n;
cout<< "Fibonacci series upto "<< n<< "is " << endl;
a = 0 ;
b = 1 ;
cout<< a<< " "<< b;
next = a + b;
while (next <= n)
{
cout << " "<< next;
a = b;
b = next;
next = a + b;
}
return 0;
}
Output:
No comments:
Post a Comment
If you have any doubt please let me know