Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

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

πŸŒ™