Print the First 10 Fibonacci Numbers in C++

This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that prints the first 10 numbers in the Fibonacci sequence.

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The sequence starts with 0 and 1, and it progresses as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. This sequence is widely used in algorithmic thinking, recursion exercises, and even in financial modeling and nature-based simulations.

In this program, you’ll use a loop to generate and print the first 10 Fibonacci numbers, making use of variables to store the previous two numbers and update them as the loop progresses.

Group

Introduction to C++

Objective

1. Initialize three integer variables to store current, previous, and next Fibonacci values.
2. Use a loop that runs 10 times to generate and print the Fibonacci numbers.
3. Update the values accordingly in each iteration to continue the sequence.
4. Display each number in the sequence on the console.

Print the first 10 Fibonacci numbers.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output

using namespace std; // Use the standard namespace

// Main function - entry point of the program
int main() {
    int n1 = 0, n2 = 1, next; // Initialize first two Fibonacci numbers and a variable for the next number

    // Print a message to indicate the start of the Fibonacci sequence
    cout << "The first 10 Fibonacci numbers are: ";

    // Loop to print the first 10 Fibonacci numbers
    for (int i = 1; i <= 10; ++i) {
        cout << n1 << " "; // Print the current Fibonacci number

        next = n1 + n2; // Calculate the next Fibonacci number
        n1 = n2;        // Move n2 to n1 for the next iteration
        n2 = next;      // Assign the new calculated value to n2
    }

    cout << endl; // Print a newline at the end

    return 0; // Return 0 to indicate successful execution
}

 Output

The first 10 Fibonacci numbers are: 0 1 1 2 3 5 8 13 21 34 

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Hello World Program in C++

    The "Hello, World!" program is one of the simplest programs to write in any programming language. It serves as an introduction to the syntax and basic functionality of a programmin...

  • Sum of Two Numbers Entered by the User in C++

    This exercise introduces user input handling in C++ through a simple program that adds two numbers provided by the user. It's an ideal starting point for understanding how to work ...

  • Convert Celsius to Fahrenheit in C++

    This beginner-friendly exercise demonstrates how to perform a temperature conversion from Celsius to Fahrenheit using a basic arithmetic formula in C++. It's an excellent introduct...

  • Calculate the Area of a Circle in C++

    This exercise helps you practice using variables, mathematical operations, and user input/output in C++ by calculating the area of a circle. The formula to compute the area of a ci...

  • Multiplication Table Generator in C++

    This exercise focuses on generating the multiplication table of a number provided by the user. It introduces the use of loops in C++, specifically the `for` loop, to repeat a set o...

  • Check if a Number is Even or Odd in C++

    This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand ...