Создание шаблона ромба в C++: несколько методов

Чтобы создать узор ромба в C++, вы можете использовать различные методы. Вот несколько подходов:

Метод 1: использование вложенных циклов

#include <iostream>
using namespace std;
int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < rows - i - 1; j++) {
            cout << " ";
        }
        for (int j = 0; j < rows; j++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Метод 2: использование рекурсии

#include <iostream>
using namespace std;
void printSpaces(int n) {
    if (n <= 0)
        return;
    cout << " ";
    printSpaces(n - 1);
}
void printStars(int n) {
    if (n <= 0)
        return;
    cout << "*";
    printStars(n - 1);
}
void printRhombus(int n, int row) {
    if (row > n)
        return;
    printSpaces(n - row);
    printStars(n);
    cout << endl;
    printRhombus(n, row + 1);
}
int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;
    printRhombus(rows, 1);
    return 0;
}

Метод 3. Использование формул

#include <iostream>
using namespace std;
int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++)
            cout << " ";
        for (int j = 1; j <= rows; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}