Anasayfa
/
Teknoloji
/
Exp 5: Write a Complete C+ Program to Display the Fibonacci Numbers. The Program Should Get the Input Specifying the Order of Fibonacci

Soru

Exp 5: Write a complete C+ program to display the Fibonacci numbers. The program should get the input specifying the order of Fibonacci number, and then display the series up to input number. Fibonacci Numbers=0,1,1,2,3,5, 13,21ldots

Çözüm

3.9 (173 Oylar)
Kerem
Uzman doğrulaması
Profesyonel · 6 yıl öğretmeni

Cevap

#include using namespace std;int main() { int n; cout > n; // Check if the input is valid if (n <= 0) { cout << "Invalid input. The order must be a positive integer." << endl; return 1; } // Display the Fibonacci series up to the input number cout << "Fibonacci Series: " << endl; for (int i = 0; i <= n; ++i) { cout << fibonacci(i) << " "; } return 0;}// Function to calculate the nth Fibonacci numberint fibonacci(int n) { if (n <= 1) { return n; } return fibonacci(n - 1) + fibonacci(n - 2);}