Write a C++ program to display first ‘n’ numbers of Fibonacci series.

Write a C++ Program to Display First ‘n’ Numbers of Fibonacci Series

The Fibonacci series is one of the most popular number sequences in mathematics and computer science. It is widely used in algorithms, recursion problems, and logical thinking exercises. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to display the first ‘n’ numbers of the Fibonacci series.

This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.

1. Introduction of Fibonacci Series

The Fibonacci series is a sequence of numbers where each number is the sum of the two previous numbers.

The series starts with:

0, 1, 1, 2, 3, 5, 8, 13, 21, …

Explanation:
First number = 0
Second number = 1
Next number = 0 + 1 = 1
Next number = 1 + 1 = 2
Next number = 1 + 2 = 3
Next number = 2 + 3 = 5

And so on.

2. Logic of the Program

To display the first ‘n’ Fibonacci numbers, follow these steps:

  1. Start the program.
  2. Declare variables for the first two numbers (0 and 1).
  3. Take the value of ‘n’ from the user.
  4. Print the first two numbers.
  5. Use a loop to calculate the next numbers.
  6. Each new number is the sum of the previous two numbers.
  7. Continue until ‘n’ numbers are printed.

Example:
If the user enters 6
The output will be:
0 1 1 2 3 5

Because:
0
1
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5

3. C++ Source Code (Easy Student Friendly Code)

#include <iostream>
using namespace std;

int main()
{
    int n, first = 0, second = 1, next;

    cout << "Enter the value of n: ";
    cin >> n;

    cout << "Fibonacci Series: ";

    for(int i = 1; i <= n; i++)
    {
        if(i == 1)
        {
            cout << first << " ";
        }
        else if(i == 2)
        {
            cout << second << " ";
        }
        else
        {
            next = first + second;
            cout << next << " ";
            first = second;
            second = next;
        }
    }

    return 0;
}

Explanation:
first and second store the first two numbers of the series.
The loop runs ‘n’ times.
Each new number is calculated by adding the previous two numbers.
The values are updated after each calculation.

4. Sample Output

Example:
Enter the value of n: 7
Fibonacci Series: 0 1 1 2 3 5 8

Example:
Enter the value of n: 5
Fibonacci Series: 0 1 1 2 3

5. Viva Questions with Answers

Q1: What is the Fibonacci series?
Answer: It is a sequence where each number is the sum of the two previous numbers.

Q2: What are the first two numbers of the Fibonacci series?
Answer: The first two numbers are 0 and 1.

Q3: Which concept is mainly used in this program?
Answer: Looping concept is mainly used in this program.

Q4: Can Fibonacci series be generated using recursion?
Answer: Yes, it can also be generated using recursion.

Q5: What is the next number after 0, 1, 1, 2, 3?
Answer: The next number is 5.

This program helps beginners understand loops, variables, and number sequences in C++. Practice more programs on cslearning.org to improve your programming knowledge.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top