For Loop in C++

 You are covering a comprehensive list of C++ topics! 🚀 Here’s a breakdown of some key for loop examples with explanations.


🔄 For Loop in C++

The for loop is used when the number of iterations is known beforehand.

🔹 Syntax

for(initialization; condition; update) {
    // Code to execute
}
  • Initialization: Sets a starting value.
  • Condition: The loop runs while this condition is true.
  • Update: Updates the loop variable in each iteration.

1️⃣ Sum of Numbers using For Loop

📌 Find the sum of the first N numbers.

#include <iostream>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        sum += i;
    }

    cout << "Sum = " << sum;
    return 0;
}

🔹 Output

Enter a number: 5
Sum = 15

2️⃣ Sum of Two Squares using For Loop

📌 Calculate the sum of squares of numbers from 1 to N.

#include <iostream>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        sum += (i * i);
    }

    cout << "Sum of squares = " << sum;
    return 0;
}

🔹 Output

Enter a number: 3
Sum of squares = 14

3️⃣ Armstrong Number using For Loop

📌 Check if a number is an Armstrong number.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num, sum = 0, temp, digit, digits = 0;
    cout << "Enter a number: ";
    cin >> num;

    temp = num;
    while (temp > 0) {
        temp /= 10;
        digits++;
    }

    temp = num;
    for (; temp > 0; temp /= 10) {
        digit = temp % 10;
        sum += pow(digit, digits);
    }

    if (sum == num)
        cout << num << " is an Armstrong number";
    else
        cout << num << " is not an Armstrong number";

    return 0;
}

🔹 Output

Enter a number: 153
153 is an Armstrong number

4️⃣ Fibonacci Series using For Loop

📌 Generate Fibonacci series up to N terms.

#include <iostream>
using namespace std;

int main() {
    int n, a = 0, b = 1, next;
    cout << "Enter number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";
    for (int i = 1; i <= n; i++) {
        cout << a << " ";
        next = a + b;
        a = b;
        b = next;
    }

    return 0;
}

🔹 Output

Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

5️⃣ Printing Star Diamond Pattern

📌 Print a diamond shape using stars (*).

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of rows: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++)
            cout << " ";
        for (int j = 1; j <= (2 * i - 1); j++)
            cout << "*";
        cout << endl;
    }

    for (int i = n - 1; i >= 1; i--) {
        for (int j = n; j > i; j--)
            cout << " ";
        for (int j = 1; j <= (2 * i - 1); j++)
            cout << "*";
        cout << endl;
    }

    return 0;
}

🔹 Output

   *
  ***
 *****
*******
 *****
  ***
   *

6️⃣ Printing Pyramid Pattern in C++

📌 Print a pyramid of * symbols.

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of rows: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++)
            cout << " ";
        for (int j = 1; j <= (2 * i - 1); j++)
            cout << "*";
        cout << endl;
    }

    return 0;
}

🔹 Output

   *
  ***
 *****
*******

7️⃣ Printing Tables using For Loop

📌 Print the multiplication table of a number.

#include <iostream>
using namespace std;

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

    for (int i = 1; i <= 10; i++) {
        cout << num << " x " << i << " = " << num * i << endl;
    }

    return 0;
}

🔹 Output

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

📌 Summary

✔ The for loop is best used when the number of iterations is known.
✔ It is useful for mathematical calculations, patterns, and loops over arrays.
✔ The loop variable updates in each iteration automatically.

Let me know if you need more examples or explanations! 🚀🔥

Post a Comment

0 Comments