C++ 스터디 #3: 구구단 출력하기

시간 2021년 4월 1일 목요일 20:30 ~ 22:10
장소 Google Meet
참가자 홍제준, 윤호연, 최원섭, 강동륜, 김도균, 김석호, 김진우, 류지원, 박경민, 심훈용, 안영현, 오성근, 우재원, 이언상, 임승환, 정대영, 정인하, 정지웅, 최민재, 함영석

구구단 2단을 출력하는 프로그램 작성하기


<fs large>출력</fs>

출력 예시처럼 2 x 1 부터 2 x 9 까지 출력한다.


<fs large>출력 예시</fs>

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18


<fs large>코드</fs>

#include <iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 9; i++)
		cout << "2 x " << i << " = " << 2 * i << endl;

	return 0;
}

정수 N을 입력받아 구구단 N단을 출력하는 프로그램 작성하기


<fs large>입력</fs>

첫째 줄에 N이 주어진다.


<fs large>출력</fs>

입출력 예시처럼 N x 1 부터 N x 9 까지 출력한다.


<fs large>입출력 예시</fs>

Enter a number : 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45


<fs large>코드</fs>

#include <iostream>
using namespace std;

int main() {
	int n;

	cout << "Enter a number : ";
	cin >> n;

	for (int i = 1; i <= 9; i++)
		cout << n << " x " << i << " = " << n * i << endl;

	return 0;
}

구구단 전체를 출력하는 프로그램 작성하기


<fs large>출력</fs>

출력 예시처럼 2 x 1 부터 9 x 9 까지 출력한다.


※ 탭(\t) 문자 사용하기


<fs large>출력 예시</fs>

2 x 1 = 2       3 x 1 = 3       4 x 1 = 4       5 x 1 = 5       6 x 1 = 6       7 x 1 = 7       8 x 1 = 8       9 x 1 = 9
2 x 2 = 4       3 x 2 = 6       4 x 2 = 8       5 x 2 = 10      6 x 2 = 12      7 x 2 = 14      8 x 2 = 16      9 x 2 = 18
2 x 3 = 6       3 x 3 = 9       4 x 3 = 12      5 x 3 = 15      6 x 3 = 18      7 x 3 = 21      8 x 3 = 24      9 x 3 = 27
2 x 4 = 8       3 x 4 = 12      4 x 4 = 16      5 x 4 = 20      6 x 4 = 24      7 x 4 = 28      8 x 4 = 32      9 x 4 = 36
2 x 5 = 10      3 x 5 = 15      4 x 5 = 20      5 x 5 = 25      6 x 5 = 30      7 x 5 = 35      8 x 5 = 40      9 x 5 = 45
2 x 6 = 12      3 x 6 = 18      4 x 6 = 24      5 x 6 = 30      6 x 6 = 36      7 x 6 = 42      8 x 6 = 48      9 x 6 = 54
2 x 7 = 14      3 x 7 = 21      4 x 7 = 28      5 x 7 = 35      6 x 7 = 42      7 x 7 = 49      8 x 7 = 56      9 x 7 = 63
2 x 8 = 16      3 x 8 = 24      4 x 8 = 32      5 x 8 = 40      6 x 8 = 48      7 x 8 = 56      8 x 8 = 64      9 x 8 = 72
2 x 9 = 18      3 x 9 = 27      4 x 9 = 36      5 x 9 = 45      6 x 9 = 54      7 x 9 = 63      8 x 9 = 72      9 x 9 = 81


<fs large>코드</fs>

#include <iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 9; i++) {
		for (int j = 2; j <= 9; j++)
			cout << j << " x " << i << " = " << j * i << "\t";
		cout << endl;
	}

	return 0;
}

정수 두 개를 입력받아 N-M단을 출력하는 프로그램 작성하기


<fs large>입력</fs>

첫째 줄에 N과 M이 주어진다.


<fs large>출력</fs>

입출력 예시처럼 2 x 1 부터 N x M 까지 출력한다.


<fs large>입출력 예시</fs>

Enter N and M : 4 7
2 x 1 = 2       3 x 1 = 3       4 x 1 = 4
2 x 2 = 4       3 x 2 = 6       4 x 2 = 8
2 x 3 = 6       3 x 3 = 9       4 x 3 = 12
2 x 4 = 8       3 x 4 = 12      4 x 4 = 16
2 x 5 = 10      3 x 5 = 15      4 x 5 = 20
2 x 6 = 12      3 x 6 = 20      4 x 6 = 24
2 x 7 = 14      3 x 7 = 25      4 x 7 = 28


<fs large>코드</fs>

#include <iostream>
using namespace std;

int main() {
	int n, m;

	cout << "Enter N and M : ";
	cin >> n >> m;

	for (int i = 1; i <= m; i++) {
		for (int j = 2; j <= n; j++)
			cout << j << " x " << i << " = " << j * i << "\t";
		cout << endl;
	}

	return 0;
}

구구단에서 홀수단은 거꾸로 출력하는 프로그램 작성하기


<fs large>출력</fs>

출력 예시처럼 2 x 1 부터 9 x 9 까지 출력한다.
홀수단은 N x 9 부터 N x 1 까지 거꾸로 출력한다.


<fs large>출력 예시</fs>

2 x 1 = 2       3 x 9 = 27      4 x 1 = 4       5 x 9 = 45      6 x 1 = 6       7 x 9 = 63      8 x 1 = 8       9 x 9 = 81
2 x 2 = 4       3 x 8 = 24      4 x 2 = 8       5 x 8 = 40      6 x 2 = 12      7 x 8 = 56      8 x 2 = 16      9 x 8 = 72
2 x 3 = 6       3 x 7 = 21      4 x 3 = 12      5 x 7 = 35      6 x 3 = 18      7 x 7 = 49      8 x 3 = 24      9 x 7 = 63
2 x 4 = 8       3 x 6 = 18      4 x 4 = 16      5 x 6 = 30      6 x 4 = 24      7 x 6 = 42      8 x 4 = 32      9 x 6 = 54
2 x 5 = 10      3 x 5 = 15      4 x 5 = 20      5 x 5 = 25      6 x 5 = 30      7 x 5 = 35      8 x 5 = 40      9 x 5 = 45
2 x 6 = 12      3 x 4 = 12      4 x 6 = 24      5 x 4 = 20      6 x 6 = 36      7 x 4 = 28      8 x 6 = 48      9 x 4 = 36
2 x 7 = 14      3 x 3 = 9       4 x 7 = 28      5 x 3 = 15      6 x 7 = 42      7 x 3 = 21      8 x 7 = 56      9 x 3 = 27
2 x 8 = 16      3 x 2 = 6       4 x 8 = 32      5 x 2 = 10      6 x 8 = 48      7 x 2 = 14      8 x 8 = 64      9 x 2 = 18
2 x 9 = 18      3 x 1 = 3       4 x 9 = 36      5 x 1 = 5       6 x 9 = 54      7 x 1 = 7       8 x 9 = 72      9 x 1 = 9


<fs large>코드</fs>

#include <iostream>
using namespace std;

int main() {
	for (int i = 1; i <= 9; i++) {
		for (int j = 2; j <= 9; j ++) {
			if (j % 2 == 0)	cout << j << " x " << i << " = " << j * i << "\t";
			else			cout << j << " x " << 10 - i << " = " << j * (10 - i) << "\t";
		}
		cout << endl;
	}

	return 0;
}

문자열과 정수를 입력받아 홀수단만 혹은 짝수단만 N개 출력하는 프로그램 작성하기


<fs large>입력</fs>

첫째 줄에 N과 문자열이 주어진다.


<fs large>출력</fs>

입력받은 문자열이 “odd”면 홀수단만, “even”이면 짝수단만 2단부터 N번째 단까지 출력한다.


※ 문자열의 자료형은 string


<fs large>입출력 예시</fs>

Enter parity and a number : odd 3
3 x 1 = 3       5 x 1 = 5       7 x 1 = 7
3 x 2 = 6       5 x 2 = 10      7 x 2 = 14
3 x 3 = 9       5 x 3 = 15      7 x 3 = 21
3 x 4 = 12      5 x 4 = 20      7 x 4 = 28
3 x 5 = 15      5 x 5 = 25      7 x 5 = 35
3 x 6 = 18      5 x 6 = 30      7 x 6 = 42
3 x 7 = 21      5 x 7 = 35      7 x 7 = 49
3 x 8 = 24      5 x 8 = 40      7 x 8 = 56
3 x 9 = 27      5 x 9 = 45      7 x 9 = 63


<fs large>코드 (1)</fs>

#include <iostream>
using namespace std;

int main() {
	int n;
	string word;

	cout << "Enter parity and a number : ";
	cin >> word >> n;

	if (word == "odd") {
		for (int i = 1; i <= 9; i++) {
			for (int j = 3; j <= 2 * n + 1; j += 2)
				cout << j << " x " << i << " = " << j * i << "\t";
			cout << endl;
		}
	}
	else if (word == "even") {
		for (int i = 1; i <= 9; i++) {
			for (int j = 2; j <= 2 * n; j += 2)
				cout << j << " x " << i << " = " << j * i << "\t";
			cout << endl;
		}
	}
	else	cout << "The word entered is neither odd nor even!" << endl;

	return 0;
}


<fs large>코드 (2)</fs>

#include <iostream>
using namespace std;

int main() {
	int n, parity = -1;
	string word;

	cout << "Enter parity and a number : ";
	cin >> word >> n;

	if (word == "odd") 	parity = 0;
	else if (word == "even") 	parity = 1;

	if (parity == -1) {
		cout << "The word entered is neither odd nor even!" << endl;
		return 0;
	}

	for (int i = 1; i <= 9; i++) {
		for (int j = 2; j <= 2 * n + 1; j ++) {
			if (j % 2 == parity) continue;
			cout << j << " x " << i << " = " << j * i << "\t";
		}
		cout << endl;
	}

	return 0;
}

  • setw(N) 함수는 N칸 만큼 공간을 확보하고 오른쪽 정렬하여 문자를 출력한다.
  • 나머지 공간은 공백으로 채워진다.
  • iomanip 헤더 파일을 포함시켜야 사용할 수 있다.
  • cout과 함께 사용한다.


<fs large>예제</fs>

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

int main() {
	cout << 1234567 << endl;
	cout << setw(4) << 4 << endl;
	cout << setw(7) << "abc";

	return 0;
}


<fs large>출력</fs>

1234567
   4
    abc

<fs large>예제</fs>

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

int main() {

	for (int i = 1; i <= 9; i++) {
		for (int j = 2; j <= 11; j++)
			cout  << j << " x " << i << " = " << j * i << "\t";
		cout << endl;
	}

	cout << endl;

	for (int i = 1; i <= 9; i++) {
		for (int j = 2; j <= 11; j++)
			cout << j << " x " << i << " = " << setw(5) << left << j * i;
		cout << endl;
	}

	return 0;
}


<fs large>출력</fs>

2 x 1 = 2       3 x 1 = 3       4 x 1 = 4       5 x 1 = 5       6 x 1 = 6       7 x 1 = 7       8 x 1 = 8       9 x 1 = 9
2 x 2 = 4       3 x 2 = 6       4 x 2 = 8       5 x 2 = 10      6 x 2 = 12      7 x 2 = 14      8 x 2 = 16      9 x 2 = 18
2 x 3 = 6       3 x 3 = 9       4 x 3 = 12      5 x 3 = 15      6 x 3 = 18      7 x 3 = 21      8 x 3 = 24      9 x 3 = 27
2 x 4 = 8       3 x 4 = 12      4 x 4 = 16      5 x 4 = 20      6 x 4 = 24      7 x 4 = 28      8 x 4 = 32      9 x 4 = 36
2 x 5 = 10      3 x 5 = 15      4 x 5 = 20      5 x 5 = 25      6 x 5 = 30      7 x 5 = 35      8 x 5 = 40      9 x 5 = 45
2 x 6 = 12      3 x 6 = 18      4 x 6 = 24      5 x 6 = 30      6 x 6 = 36      7 x 6 = 42      8 x 6 = 48      9 x 6 = 54
2 x 7 = 14      3 x 7 = 21      4 x 7 = 28      5 x 7 = 35      6 x 7 = 42      7 x 7 = 49      8 x 7 = 56      9 x 7 = 63
2 x 8 = 16      3 x 8 = 24      4 x 8 = 32      5 x 8 = 40      6 x 8 = 48      7 x 8 = 56      8 x 8 = 64      9 x 8 = 72
2 x 9 = 18      3 x 9 = 27      4 x 9 = 36      5 x 9 = 45      6 x 9 = 54      7 x 9 = 63      8 x 9 = 72      9 x 9 = 81

2 x 1 = 2    3 x 1 = 3    4 x 1 = 4    5 x 1 = 5    6 x 1 = 6    7 x 1 = 7    8 x 1 = 8    9 x 1 = 9
2 x 2 = 4    3 x 2 = 6    4 x 2 = 8    5 x 2 = 10   6 x 2 = 12   7 x 2 = 14   8 x 2 = 16   9 x 2 = 18
2 x 3 = 6    3 x 3 = 9    4 x 3 = 12   5 x 3 = 15   6 x 3 = 18   7 x 3 = 21   8 x 3 = 24   9 x 3 = 27
2 x 4 = 8    3 x 4 = 12   4 x 4 = 16   5 x 4 = 20   6 x 4 = 24   7 x 4 = 28   8 x 4 = 32   9 x 4 = 36
2 x 5 = 10   3 x 5 = 15   4 x 5 = 20   5 x 5 = 25   6 x 5 = 30   7 x 5 = 35   8 x 5 = 40   9 x 5 = 45
2 x 6 = 12   3 x 6 = 18   4 x 6 = 24   5 x 6 = 30   6 x 6 = 36   7 x 6 = 42   8 x 6 = 48   9 x 6 = 54
2 x 7 = 14   3 x 7 = 21   4 x 7 = 28   5 x 7 = 35   6 x 7 = 42   7 x 7 = 49   8 x 7 = 56   9 x 7 = 63
2 x 8 = 16   3 x 8 = 24   4 x 8 = 32   5 x 8 = 40   6 x 8 = 48   7 x 8 = 56   8 x 8 = 64   9 x 8 = 72
2 x 9 = 18   3 x 9 = 27   4 x 9 = 36   5 x 9 = 45   6 x 9 = 54   7 x 9 = 63   8 x 9 = 72   9 x 9 = 81

  • activity/public/2021/cpp/210401.txt
  • 마지막으로 수정됨: 6년 전
  • 저자 david