Write a program in C++ to display the first n terms of Fibonacci series

  • برمجة سي بلس بلس
  • برمجة

Write a program in C++ to display the first n terms of Fibonacci series

Sample Output:

 Display the first n terms of Fibonacci series:                        
---------------------------------------------------                    
 Input number of terms to  display: 10                                 
 Here is the Fibonacci series upto  to 10 terms:                       
0 1 1 2 3 5 8 13 21 34 

الأجوبة

#include <iostream>
using namespace std;

int main()
{
    int prv = 0, pre = 1, trm, i, n;
    cout << "\n\n Display the first n terms of Fibonacci series:\n";
    cout << "---------------------------------------------------\n";
    cout << " Input number of terms to  display: ";
    cin >> n;
    cout << " Here is the Fibonacci series upto  to " << n << " terms: "<<endl;
    cout << prv << " " << pre;
    for (i = 3; i <= n; i++) 
    {
        trm = prv + pre;
        cout << " " << trm;
        prv = pre;
        pre = trm;
    }
    cout << endl;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...