[C++] 다중for문을 써서 별을 찍어보자

2022. 5. 31. 00:01코딩 1막 <C++개념편>/코딩 1막 <C++응용편>

728x90

6가지 별찍기 문제

 

#include <iostream>

using namespace std;


void main()
{
	cout << "1." << endl;
	for (int j = 1; j <= 5; j++)
	{
		for (int i = 1; i <= 5; i++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl << endl;

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

	cout << "3." << endl;
	for (int j = 5; j >= 1; j--)
	{
		for (int i = 1; i <= j; i++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl << endl;

	cout << "4." << endl;
	for (int j = 1; j <= 5; j++)
	{
		for (int i = 1; i <= 5; i++)
		{
			if (i <= 5 - j)
			{
				cout << " ";
			}
			else
				cout << "*";
		}
		cout << endl;
	}
	cout << endl << endl;

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

	cout << "6." << endl;
	for (int i = 0; i <= 5; i += 2)
	{
		for (int k = 0; k < i; k += 2)
		{
			cout << " ";
		}
		for (int j = 0; j < 5 - i; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}

 

출력 결과

 

728x90