{
 "metadata": {
  "name": "",
  "signature": "sha256:7be64d50608515e43ddcf6d9a0311be1c6e260d1acc2eec7e9577015cf19d4da"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 6: Loops"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example square.c, Page 102"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    print \"This program prints a table of squares.\"\n",
      "    n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
      "    i=1 #variable declaration\n",
      "    while(i<=n):\n",
      "        print \"%10d%10d\" % (i,i * i) #printing number and it's square\n",
      "        i=i+1\n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This program prints a table of squares.\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number of entries in table: 5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "         1         1\n",
        "         2         4\n",
        "         3         9\n",
        "         4        16\n",
        "         5        25\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example sum.c, Page 103"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    sum=0\n",
      "    print \"This program sums a series of integers.\"\n",
      "    n=int(raw_input(\"Enter integers (0 to terminate): \")) #input the integers to operate on\n",
      "    while(n!=0):\n",
      "        sum=sum+n #calculating sum till 0 encountered\n",
      "        n=input()\n",
      "    print \"The sum is: %d\" % sum #printing sum\n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This program sums a series of integers.\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter integers (0 to terminate): 8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "23\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "71\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The sum is: 107\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example numdigits.c, Page 105"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    digits=0 #initialise\n",
      "    n=int(raw_input(\"Enter a nonnegative integer: \")) #input the number\n",
      "    n=n/10\n",
      "    digits=digits+1\n",
      "    #finding number of digits\n",
      "    while(n>0):\n",
      "        n=n/10\n",
      "        digits=digits+1\n",
      "    print \"The number has %d digit(s).\" % digits #printing answer\n",
      "\n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a nonnegative integer: 60\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The number has 2 digit(s).\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example square2.c, Page 110"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    print \"This program prints a table of squares.\" \n",
      "    n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
      "    for i in range (1,(n+1)):\n",
      "        print \"%10d%10d\" % (i,i * i) #printing number and it's square\n",
      "        \n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This program prints a table of squares.\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number of entries in table: 5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "         1         1\n",
        "         2         4\n",
        "         3         9\n",
        "         4        16\n",
        "         5        25\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example square3.c,Page 110"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    print \"This program prints a table of squares.\" \n",
      "    n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
      "    \n",
      "    #variable declaration\n",
      "    i=1\n",
      "    odd=3\n",
      "    square=1\n",
      "    #calculation\n",
      "    for i in range (1,(n+1)):\n",
      "        print \"%10d%10d\" % (i,square)\n",
      "        square=square+odd \n",
      "        odd=odd+2\n",
      "        \n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This program prints a table of squares.\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter number of entries in table: 5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "         1         1\n",
        "         2         4\n",
        "         3         9\n",
        "         4        16\n",
        "         5        25\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example checking.c, Page 115"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    balance=0.0 #initialise\n",
      "    print \"*** ACME checkbook-balancing program ***\"\n",
      "    print \"Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\"\n",
      "    print \"\"\n",
      "    while(1):\n",
      "        cmd=int(raw_input(\"Enter command: \")) \n",
      "        \n",
      "        #operate on balance according to the option selected\n",
      "        if cmd==0:\n",
      "            balance=0.0 #clear balance\n",
      "        elif cmd==1:\n",
      "            credit=float(raw_input(\"Enter amount of credit: \"))\n",
      "            balance=balance+credit #credit\n",
      "        elif cmd==2:\n",
      "            debit=float(raw_input(\"Enter amount of debit: \"))\n",
      "            balance=balance-debit #debit\n",
      "        elif cmd==3:\n",
      "            print \"Current balance: $%0.2f\" % balance #show current balance\n",
      "        elif cmd==4:\n",
      "            return 0 #exit\n",
      "        else:\n",
      "            print \"Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\" \n",
      "            \n",
      "        \n",
      "if  __name__=='__main__':\n",
      "    main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "*** ACME checkbook-balancing program ***\n",
        "Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of credit: 1042.56\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of debit: 133.79\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of credit: 1754.32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of debit: 1400\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of debit: 68\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter amount of debit: 50\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Current balance: $1145.09\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter command: 4\n"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}