본문 바로가기

Development/C / C++

[C언어 연습문제] 숫자 피라미드

Windows 7 64bit / Visual Studio 2013 Express

 

 

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int height = -1;
    int lineNumber = 0;
    int numberPrint = 1;
    int variation = 1;
    int mid = 0;
    int numberCount = 0;

    printf_s("Input height : ");
    scanf_s("%d", &height);

    for (int heightCount = 1; heightCount < height; heightCount++)
    {
        numberCount = 0;
        numberPrint = heightCount;

        

        for (int spaceCount = 0; spaceCount < height - heightCount; spaceCount++)
        {
            printf_s(" ");
            numberCount++;
        }

        for (int widthCount = 0; widthCount < (heightCount * 2) - 1; widthCount++)
        {
            if (numberPrint >= 10)
            {
                numberPrint = numberPrint % 10;
            }
            else if (numberPrint < 0)
            {
                numberPrint = 9;
            }

            mid = (heightCount * 2) - 1;
            printf_s("%d", numberPrint);
            numberCount++;

            if (numberCount >= height)
            {
                variation = -1;
            }
            
            numberPrint += variation;
        }
        printf_s("\n");
        variation = 1;
    }

    return 0;
}

Output