Ana sayfa
/
Teknoloji
/
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

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

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.9173 Voting
avatar
Kerem
Profesyonel · 6 yıl öğretmeni
Uzman doğrulaması

Cevap

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