C++ 숫자를 이용한 반 피라미드 패턴

C++Beginner
지금 연습하기

소개

이 랩에서는 C++ 프로그래밍 언어를 사용하여 숫자로 반 피라미드 구조를 출력하는 방법을 배우게 됩니다. 중첩 루프 구조를 사용하여 각 행의 행과 열의 수를 반복하고 결정합니다.

프로젝트 디렉토리에 새로운 C++ 파일 생성

터미널에서 cd 명령을 사용하여 프로젝트 디렉토리로 이동한 다음, touch half_pyramid_numbers.cpp 명령을 사용하여 "half_pyramid_numbers.cpp"라는 새로운 C++ 파일을 생성합니다.

cd ~/project
touch half_pyramid_numbers.cpp

코드 입력

숫자를 사용하여 반 피라미드 구조를 출력하기 위해 "half_pyramid_numbers.cpp" 파일에 다음 코드를 입력합니다.

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the Half Pyramid using Numbers Program :-)\n\n\n";
    cout << " =====  Program to print a Half Pyramid using numbers ===== \n\n";

    //i to iterate the outer loop and j for the inner loop
    int i, j, rows;

    //prompt the user to enter the number of rows
    cout << "Enter the number of rows in the pyramid: ";
    cin >> rows;

    //outer loop is used to move to a particular row
    for (i = 1; i <= rows; i++)
    {
        //inner loop is used to decide the number of columns in a particular row
        for (j = 1; j <= i; j++)
        {
            cout << j << " "; //printing the column number within each row
        }

        cout << endl; //move to the next line after each row is printed
    }

    cout << "\n\n";

    return 0; //end the program
}

요약

축하합니다! C++ 에서 숫자를 사용한 반 피라미드 패턴 Lab 을 성공적으로 완료했습니다. 이 Lab 에서는 중첩 루프 구조를 사용하여 숫자를 이용한 반 피라미드 구조를 출력하는 방법을 배웠습니다. 이제 더 복잡한 패턴과 구조로 넘어갈 준비가 되었습니다.