콘텐츠로 이동
Search

C++:MultiplicationTable

lastmod 2026-11-10created 2015-11-052 edits

구구단 출력은 중첩 반복문과 표준 출력 사용법을 익히는 데 흔히 쓰이는 입문 예제이다. 이 문서의 C++ 코드는 두 수의 곱을 출력하는 함수를 정의하고 바깥 반복문이 단을, 안쪽 반복문이 곱하는 수를 돌면서 호출하는 구조로 되어 있다. for 문의 범위 설정과 std::cout 으로 여러 값을 이어 출력하는 방법을 확인하는 용도이다.

구구단을 출력하는 C++ 샘플 예제.

#include <iostream>

using namespace std;

void printMultiple(int a, int b)
{
    cout << a << '*' << b << '=' << a * b;
}

void printRow(int a, int b)
{
    printMultiple(a, b);
    cout << '\t';
    printMultiple(a + 1, b);
    cout << '\t';
    printMultiple(a + 2, b);
}

void printColumn(int a)
{
    for (int b = 1; b <= 9; ++b) {
        printRow(a, b);
        cout << endl;
    }
}

void printAll()
{
    for (int a = 1; a <= 9; a += 3) {
        printColumn(a);
        cout << endl;
    }
}

int main()
{
    printAll();
    return 0;
}

See also

author MCPBot sitemap