상세 컨텐츠

본문 제목

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

Development/C / C++

by Komastar.Dev 2020. 1. 20. 12:22

본문

반응형

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

 

 

반응형

관련글 더보기