Here is a sample C++ program that will display the Fibonacci series up to the specified number provided by the user.
In this example, the user has entered
10 as the number of terms for the Fibonacci series. The program then calculates and displays the series up to the 10th term, which is 34. The output shows each number in the series separated by a space.#include <iostream>
using namespace std;
int main() {
int num;
int first = 0, second = 1, next;
cout << "Enter the number of terms for Fibonacci series: ";
cin >> num;
cout << "Fibonacci Series up to " << num << " terms: " << endl;
for (int i = 0; i < num; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
cout << next << " ";
}
return 0;
}
//===========
OUTPUT:
Enter the number of terms for Fibonacci series: 10
Fibonacci Series up to 10 terms:
0 1 1 2 3 5 8 13 21 34
Explanation:
- First, we include the iostream library to use standard input and output functions.
- The using namespace std; statement is used to avoid using std:: before every cout and cin statement.
- We declare the num variable to store the number of terms of the Fibonacci series provided by the user.
- We declare the first, second, and next variables to store the first, second, and next numbers of the series respectively.
- The user is asked to enter the number of terms for the Fibonacci series.
- The for loop is used to iterate through the series and display it.
- If the current iteration is either the first or second, the value of next is set to the current iteration number (0 or 1).
- Otherwise, the value of next is calculated by adding the previous two numbers in the series.
- Then, the values of first and second are updated for the next iteration.
- Finally, the value of next is displayed for each iteration of the loop.
Notes:
- The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. The first two numbers in the series are 0 and 1, and every number after that is the sum of the previous two numbers. The series goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
- In this program, we use a for loop to iterate through the series up to the specified number of terms provided by the user.
- We use a conditional statement (if-else) to determine the value of next for each iteration of the loop.
- We use the variables first, second, and next to calculate the Fibonacci series by adding the previous two numbers in the series.
- We display the Fibonacci series using the cout statement in the loop.
- It is important to note that the Fibonacci sequence can quickly become very large, so care should be taken to avoid integer overflow when computing large values in the series.
- One way to avoid this is to use a different data type, such as long long, which can store larger integers than int.
- Additionally, the user input should be validated to ensure that it is a positive integer, since negative or non-integer input may cause unexpected behavior in the program.
- The using namespace std; statement is generally discouraged in larger projects because it can cause naming conflicts. A better practice is to use the std:: prefix before standard library functions to avoid conflicts.
- The return 0; statement at the end of the main function is used to indicate that the program has executed successfully.
No comments:
Post a Comment