소개
이 랩에서는 C++ 프로그래밍 언어를 사용하여 숫자로 역 반 피라미드 구조를 출력하는 방법을 배웁니다. 중첩 루프 구조를 사용하여 패턴을 반복하고 출력합니다.
이 랩에서는 C++ 프로그래밍 언어를 사용하여 숫자로 역 반 피라미드 구조를 출력하는 방법을 배웁니다. 중첩 루프 구조를 사용하여 패턴을 반복하고 출력합니다.
터미널로 이동하여 다음 명령을 사용하여 ~/project 디렉토리에 main.cpp라는 새 파일을 생성합니다.
touch ~/project/main.cpp
파일을 생성한 후 텍스트 편집기를 사용하여 엽니다.
main.cpp 파일에 다음 코드를 추가합니다.
#include <iostream>
using namespace std;
int main()
{
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to print a Reverse Half Pyramid using Numbers ===== \n\n";
//i to iterate the outer loop and j for the inner loop
int i, j, rows;
//to denote the range of numbers in each row
int last;
cout << "Enter the number of rows in the pyramid: ";
cin >> rows;
cout << "\n\nThe required Reverse Pyramid pattern containing " << rows << " rows is:\n\n";
//outer loop is used to move to a particular row
for (i = 1; i <= rows; i++)
{
//to display that the outer loop maintains the row number
cout << "Row ## " << i << " contains numbers from 1 to " << (rows - i + 1) << " : ";
last = rows -i + 1;
//inner loop is used to decide the number of * in a particular row
for (j = 1; j<= last; j++)
{
cout << j << " ";
}
cout << endl;
}
cout << "\n\n";
return 0;
}
다음 명령을 사용하여 코드를 컴파일하고 실행합니다.
g++ main.cpp -o main
./main
다음 출력을 볼 수 있습니다.
Welcome to LabEx :-)
===== Program to print a Reverse Half Pyramid using Numbers =====
Enter the number of rows in the pyramid: 6
The required Reverse Pyramid pattern containing 6 rows is:
Row ## 1 contains numbers from 1 to 6 : 1 2 3 4 5 6
Row ## 2 contains numbers from 1 to 5 : 1 2 3 4 5
Row ## 3 contains numbers from 1 to 4 : 1 2 3 4
Row ## 4 contains numbers from 1 to 3 : 1 2 3
Row ## 5 contains numbers from 1 to 2 : 1 2
Row ## 6 contains numbers from 1 to 1 : 1
이 랩에서는 C++ 프로그래밍 언어에서 중첩 루프를 사용하여 숫자로 역 반 피라미드 구조를 출력하는 방법을 배웠습니다.