Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C++ Program to Find Fibonacci Number

 

C++ Program to Find Fibonacci Number

✅ C++ Program to Find Fibonacci Number (Iterative Method)


#include <iostream>
using namespace std;

int fib(int n)
{
    if (n == 0) return 0;
    if (n == 1) return 1;

    int a = 0, b = 1, c;

    for (int i = 2; i <= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}

int main()
{
    int n;
    cout << "Enter a number: ";
    cin >> n;

    cout << "Fibonacci number = " << fib(n);
    return 0;
}
  

πŸ“˜ Explanation:

This C++ program calculates the Fibonacci number for a given input using the iterative approach.

The Fibonacci sequence starts as: 0, 1, 1, 2, 3, 5, 8, ...

Each number is the sum of the previous two numbers. This program avoids recursion and uses a loop, which makes it more efficient in terms of time and memory.

🧾 Sample Output:

Enter a number:
7
Fibonacci number = 13
  

πŸ”‘ Keywords:

C++ Fibonacci program, Fibonacci number in C++, iterative Fibonacci, C++ loop programs, number series in C++

πŸ” Search Description:

Learn how to find Fibonacci number in C++ using an efficient iterative method. Includes full program, explanation, and output.

πŸ“Œ Hashtags:

#CPlusPlus #Fibonacci #CPPPrograms #ProgrammingBasics #DSA #1printf

Comments

Popular Posts

πŸŒ™