{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# while\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int row, col;\n", " char char1;\n", " row = col = 0;\n", " \n", " while(row < 5)\n", " {\n", " char1 = 'A';\n", " while(col <= row)\n", " {\n", " printf(\"%c\", char1+col);\n", " col++;\n", " }\n", " row++;\n", " col = 0;\n", " printf(\"\\n\");\n", " }\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "다음 프로그램을 실행 한 후의 j의 값을 예상해보자" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int i, k, j = 0;\n", " for(i = 1; i <= 3; i++)\n", " {\n", " k = 1;\n", " while(K <= 3)\n", " {\n", " j + j + i;\n", " k++;\n", " printf(\"%d\", j);\n", " }\n", " }\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제 \n", "다음과 같은 코드가 있다. 코드를 이해하고`while`문으로 변환 해보자" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " for (cnt = 0, i = 1, j =2; cnt < n; ++cnt, i += 2, j += 2)\n", " odd += i, even += j;\n", "\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "제곱 계산 결과를 나타내는 표를 `while`문으로 만들려고 한다. 사용자가 값을 입력하면 1부터 그 값까지의 제곱 결과를 표시 해 보자. 각 숫자를 표현하기 위해 열자리를 확보하도록 `printf`문의 형식지정자를 조정하기 바랍니다.\n", "\n", "실행 결과 예:\n", "\n", " This program prints a table of squares.\n", " Enter number of entries in table: 5\n", " 1 1\n", " 2 4\n", " 3 9\n", " 4 16\n", " 5 25\n", " \n", "`for`문으로 해결한 경우와 비교해서 어떤 방식이 더 좋은가? 어떤 이유 때문에 더 좋다고 생각하는가?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " /* Prints a table of squares using a while statement */\n", " \n", "#include \n", " \n", "int main(void)\n", "{\n", " // your code here\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "사용자가 0을 입력하기 전까지 입력한 숫자들의 합을 구하는 프로그램을 작성 해보자. 힌트: 반복문 내에 `scanf`를 넣어 값을 읽음\n", "\n", "실행 결과의 예:\n", "\n", " This program sums a series of integers.\n", " Enter integers (0 to terminate): 8 23 71 5 0\n", " The sum is: 107\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/* Sums a series of numbers */\n", " \n", "#include \n", " \n", "int main(void)\n", "{\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 문제\n", "\n", "사용자가 입력한 어떤 숫자의 자릿수를 출력하는 프로그램을 `do... while`문을 사용하여 작성하고자 한다. \n", "\n", "\n", "힌트: 입력한 숫자를 0이 될 때까지 10으로 계속 나눈 횟수를 카운트 하면됨\n", "\n", "실행 결과의 예:\n", "\n", " Enter a nonnegative integer: 60\n", " The number has 2 digit(s)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "/* Calculates the number of digits in an integer */\n", "\n", "#include \n", "\n", "int main(void)\n", "{\n", " // your code here\n", " return 0;\n", "}\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "C", "language": "c", "name": "c" }, "language_info": { "file_extension": ".c", "mimetype": "text/plain", "name": "c" } }, "nbformat": 4, "nbformat_minor": 2 }