{
 "metadata": {
  "name": "",
  "signature": "sha256:afd37f358b4faba0dd82fd8fdf176be8389b2a27c7cdec9882b3da534d4a3ec8"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 4: Control Structures"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example if1.c, page no. 101"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "i = int(raw_input(\"Enter a number: \")) \n",
      "if i % 3 == 0 :\n",
      "    print \"Number entered is divisible by 3.\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number: 30\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Number entered is divisible by 3.\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example scanf8.c, page no. 102"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#The output in the textbok is 0.000000 because it was not able to convert string to float in C\n",
      "#Hence, by default it stored 0.0000000. In Python the same would give error. Hence, the answer differs\n",
      "\n",
      "years = float(raw_input(\"Input your age in years: \"))\n",
      "secs = years * 365 * 24 * 60 * 60\n",
      "print \"You have livid for %f seconds \" %secs"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input your age in years: 25\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You have livid for 788400000.000000 seconds \n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example if2.c, page no. 102"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#the way it is given in textbook is not possible in Python. Doing it in different manner\n",
      "\n",
      "print \"Input your age in years: \",\n",
      "years =(raw_input())\n",
      "try:\n",
      "    float(years)\n",
      "    secs = float(years) * 365 * 24 * 60 * 60\n",
      "    print \"you have lived for %f seconds\" %secs\n",
      "except:\n",
      "    print \"the input is not a floating-point number\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input your age in years: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " you have lived for 2365200000.000000 seconds\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example if3.c, page no. 103"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#the program will remain same as the previous one\n",
      "\n",
      "print \"Input your age in years: \",\n",
      "years =(raw_input())\n",
      "try:\n",
      "    float(years)\n",
      "    secs = float(years) * 365 * 24 * 60 * 60\n",
      "    print \"you have lived for %f seconds\" %secs\n",
      "except:\n",
      "    print \"the input is not a floating-point number\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input your age in years: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "sundeep\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " the input is not a floating-point number\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example large.c, page no. 105"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Input a b c\"\n",
      "a = float(raw_input(\"a=\"))\n",
      "b = float(raw_input(\"b=\"))\n",
      "c = float(raw_input(\"c=\"))\n",
      "big = a\n",
      "if b>big :\n",
      "    big = b\n",
      "if c>big :\n",
      "    big = c\n",
      "print \"Largest of the three numbers = %7.2f\" %big"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input a b c\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "a=10\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b=20\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c=45\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Largest of the three numbers =   45.00\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example ifelse1.c, page no. 105"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#the program will remain same as the previous one\n",
      "\n",
      "print \"Input your age in years: \",\n",
      "years =(raw_input())\n",
      "try:\n",
      "    float(years)\n",
      "    secs = float(years) * 365 * 24 * 60 * 60\n",
      "    print \"you have lived for %f seconds\" %secs\n",
      "except:\n",
      "    print \"the input is not a floating-point number\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input your age in years: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "100\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " you have lived for 3153600000.000000 seconds\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example ifelse2.c, page no. 107"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "input1 = int(raw_input(\"Input an integer: \"))\n",
      "if input1 :\n",
      "    print \"It is non-zero.\"\n",
      "else :\n",
      "    print \"it is zero.\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input an integer: 12\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "It is non-zero.\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example scope.c, page no. 112"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#The block given in the textbook is not possible in Python. Here the value will remain the same.\n",
      "\n",
      "i = 10\n",
      "print \"In main. i is %d \" %i\n",
      "i = 20\n",
      "print \"In compound statement. i is %d\" %i\n",
      "i = i+1\n",
      "print \"After incrementing: i is %d\" %i\n",
      "print \"In main again. i is %d\" %i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "In main. i is 10 \n",
        "In compound statement. i is 20\n",
        "After incrementing: i is 21\n",
        "In main again. i is 21\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example for.c, page no. 113"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for i in range(1, 11):\n",
      "    print i*5,"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5 10 15 20 25 30 35 40 45 50\n"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example sumsq1.c, page no. 115"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum = 0\n",
      "sum_of_squares = 0\n",
      "for i in range(2, 31, 2):\n",
      "    sum += i\n",
      "    sum_of_squares += i*i\n",
      "print \"Sum of first 15 positive even numbers = \", sum\n",
      "print \"Sum of their squares = \", sum_of_squares"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum of first 15 positive even numbers =  240\n",
        "Sum of their squares =  4960\n"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example sumsq2.c, page no. 116"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum = 0\n",
      "sum_of_squares = 0\n",
      "for i in range(30, 1, -2):\n",
      "    sum += i\n",
      "    sum_of_squares += i*i\n",
      "print \"Sum of first 15 positive even numbers = \", sum\n",
      "print \"Sum of their squares = \", sum_of_squares"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum of first 15 positive even numbers =  240\n",
        "Sum of their squares =  4960\n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example for2.c, page no. 116"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for i in range(1, 11):\n",
      "    print 5*i,"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5 10 15 20 25 30 35 40 45 50\n"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.1, page no. 117"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#error in textbook, i should be initialized by 1\n",
      "\n",
      "sign = 1\n",
      "print \"Input X and N: \"\n",
      "x = float(raw_input(\"X: \"))\n",
      "n = int(raw_input(\"N: \"))\n",
      "x *= 3.1412/180.0\n",
      "sum, t = x, x\n",
      "for i in range(1, n):\n",
      "    sign *=-1\n",
      "    t = sign * x * x / (2*i*(2*i+1))\n",
      "    sum += t\n",
      "print \"SIN(X) = %6.2f\" %sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input X and N: \n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X: 60\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "N: 12\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "SIN(X) =   0.90\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.2, page no. 118"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "num = 0\n",
      "sum = 0\n",
      "print \"Input the marks, -1 at the end\"\n",
      "i = int(raw_input())\n",
      "while i != -1:\n",
      "    sum += i\n",
      "    num += 1\n",
      "    i = int(raw_input())\n",
      "average = sum / num\n",
      "print \"The average is %.2f\" %average"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the marks, -1 at the end\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "12\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "14\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "16\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "18\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-1\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The average is 15.00\n"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example binary.c, page no. 120"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "binary = raw_input(\"Input the binary number: \")\n",
      "decimal = int(binary, 2)\n",
      "print \"The decimal equivalent of binary number %s is %d\" %(binary, decimal)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the binary number: 1101\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The decimal equivalent of binary number 1101 is 13\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example dowhile.c, page no. 122"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#there is no do while loop in Python. We will use simple while loop\n",
      "inchar = ''\n",
      "while(inchar != 'y' and inchar != 'n'):\n",
      "    inchar = raw_input(\"Input y or n: \")\n",
      "if inchar == 'y':\n",
      "    print \"you pressed y.\"\n",
      "if inchar == 'n':\n",
      "    print \"you pressed n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input y or n: j\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input y or n: j\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input y or n: n\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "you pressed n\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.4, page no. 123"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "num = int(raw_input(\"Input the number: \"))\n",
      "n = num\n",
      "sum = 0\n",
      "rev = 0\n",
      "while(num != 0):\n",
      "    digit = num % 10\n",
      "    sum += digit\n",
      "    rev = rev * 10 + digit\n",
      "    num /= 10\n",
      "print \"Sum of the digits of the number = %4d\" %sum\n",
      "print \"Reverse of the number = %7d\" %rev\n",
      "if n == rev:\n",
      "    print \"The number is a palindrome\"\n",
      "else:\n",
      "    print \"The number is not a palindrome\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the number: 1221\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum of the digits of the number =    6\n",
        "Reverse of the number =    1221\n",
        "The number is a palindrome\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example iflese.c, page no. 127"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "print \"Choice of destinations: \"\n",
      "print \"\\t1 - Mercury\"\n",
      "print \"\\t2 - Venus\"\n",
      "print \"\\t3 - Mars\"\n",
      "choice = int(raw_input(\"Enter the number corresponding to your choice: \"))\n",
      "if choice == 1:\n",
      "    print \"Mercury is closest to the sun.\"\n",
      "    print \"So the weather may be quite hot there.\"\n",
      "    print \"The journey will cost you 5200 IGCs.\"\n",
      "elif choice == 2:\n",
      "    print \"Venus is the second planet from the sun.\"\n",
      "    print \"The weather may be hot and poisonous.\"\n",
      "    print \"The journey will cost you 3200 IGCs.\"\n",
      "elif choice == 3:\n",
      "    print \"Mars is the closest planet to the earth.\"\n",
      "    print \"The weather has been excellent till now.\"\n",
      "    print \"The journey will cost you 1200 IGCs.\"\n",
      "else:\n",
      "    \"Unknown destination. Unpredictable IGCs\"\n",
      "print \"Note: IGC = Ingeer-Galactic Currency\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Choice of destinations: \n",
        "\t1 - Mercury\n",
        "\t2 - Venus\n",
        "\t3 - Mars\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number corresponding to your choice: 2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Venus is the second planet from the sun.\n",
        "The weather may be hot and poisonous.\n",
        "The journey will cost you 3200 IGCs.\n",
        "Note: IGC = Ingeer-Galactic Currency\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.5, page no. 128"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#there is no switch case in Python, hence, no use of break in if conditions. So, the program will remain same\n",
      "\n",
      "print \"Choice of destinations: \"\n",
      "print \"\\t1 - Mercury\"\n",
      "print \"\\t2 - Venus\"\n",
      "print \"\\t3 - Mars\"\n",
      "choice = int(raw_input(\"Enter the number corresponding to your choice: \"))\n",
      "if choice == 1:\n",
      "    print \"Mercury is closest to the sun.\"\n",
      "    print \"So the weather may be quite hot there.\"\n",
      "    print \"The journey will cost you 5200 IGCs.\"\n",
      "elif choice == 2:\n",
      "    print \"Venus is the second planet from the sun.\"\n",
      "    print \"The weather may be hot and poisonous.\"\n",
      "    print \"The journey will cost you 3200 IGCs.\"\n",
      "elif choice == 3:\n",
      "    print \"Mars is the closest planet to the earth.\"\n",
      "    print \"The weather has been excellent till now.\"\n",
      "    print \"The journey will cost you 1200 IGCs.\"\n",
      "else:\n",
      "    \"Unknown destination. Unpredictable IGCs\"\n",
      "print \"Note: IGC = Ingeer-Galactic Currency\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Choice of destinations: \n",
        "\t1 - Mercury\n",
        "\t2 - Venus\n",
        "\t3 - Mars\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number corresponding to your choice: 3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mars is the closest planet to the earth.\n",
        "The weather has been excellent till now.\n",
        "The journey will cost you 1200 IGCs.\n",
        "Note: IGC = Ingeer-Galactic Currency\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.6, page no. 130"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Input a b & c\"\n",
      "a = float(raw_input(\"a: \"))\n",
      "b = float(raw_input(\"b: \"))\n",
      "c = float(raw_input(\"c: \"))\n",
      "if a != 0:\n",
      "    disc = b * b - 4 * a * c\n",
      "    print \"Discriminant = %5.2f\" %disc\n",
      "    if disc < 0:\n",
      "        k = 1\n",
      "    elif disc == 0:\n",
      "        k = 2\n",
      "    elif disc > 0:\n",
      "        k = 3\n",
      "    if k == 1:\n",
      "        print \"Roots are imaginary\"\n",
      "        real = -b/(2*a)\n",
      "        disc = -disc\n",
      "        num = pow(disc, 0.5)\n",
      "        imag = num/(2*a)\n",
      "        print \"Root1 = %5.2f +j %5.2f\" %(real, imag)\n",
      "        print \"Root2 = %5.2f -j %5.2f\" %(real, imag)\n",
      "    elif k == 2:\n",
      "        print \"Roots are real equal\"\n",
      "        root1 = -b/(2*a)\n",
      "        print \"Root1 = Root2 = %7.2f\" %root1\n",
      "    elif k == 3:\n",
      "        print \"Roots are real and unequal\"\n",
      "        root1 = (-b + sqrt(disc))/(2*a)\n",
      "        root2 = (-b - sqrt(disc))/(2*a)\n",
      "        print \"Root1 = %7.2f Root2 = %7.2f\" %(root1, root2)\n",
      "else:\n",
      "   print \"Equation is linear\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input a b & c\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "a: 1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b: 2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c: 7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Discriminant = -24.00\n",
        "Roots are imaginary\n",
        "Root1 = -1.00 +j  2.45\n",
        "Root2 = -1.00 -j  2.45\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.7, page no. 132"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum = 0\n",
      "for i in range(5):\n",
      "    num = int(raw_input(\"Enter an integer: \"))\n",
      "    if num < 0:\n",
      "        print \"You have entered a negative number\"\n",
      "        continue\n",
      "    sum += num\n",
      "print \"The sum of positive integers entered = \", sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter an integer: 10\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter an integer: -15\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You have entered a negative number\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter an integer: 15\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter an integer: -100\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You have entered a negative number\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter an integer: 30\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The sum of positive integers entered =  55\n"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.8, page no. 133"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Input X & N\"\n",
      "x = float(raw_input(\"X: \"))\n",
      "n = int(raw_input(\"N: \"))\n",
      "t = 1\n",
      "sum = 1\n",
      "for i in range(1, n):\n",
      "    ifact = i\n",
      "    t = t*x/ifact\n",
      "    sum +=t\n",
      "print \"E raies to power x = %10.4f\" %sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input X & N\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "X: 1.00\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "N: 2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "E raies to power x =     2.0000\n"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.9, page no. 134"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "n = int(raw_input(\"Enter the number: \"))\n",
      "if n == 0:\n",
      "    print \"The factorial of 0 is 1\"\n",
      "else:\n",
      "    fact = 1\n",
      "    for i in range(1, n+1):\n",
      "        fact = fact * i\n",
      "    print \"Factorial of %4d is %5d\" %(n, fact)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number: 5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Factorial of    5 is   120\n"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.10, page no. 135"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "binom = 1\n",
      "q = 0\n",
      "p = int(raw_input(\"Input the number of rows: \"))\n",
      "print \"Pascal Triangle\"\n",
      "while q < p:\n",
      "    for r in range(40-3*q, 0, -1):\n",
      "        print \" \",\n",
      "    for x in range(0, q+1):\n",
      "        if x == 0 or q == 0:\n",
      "            binom = 1\n",
      "        else:\n",
      "            binom = (binom*(q-x+1))/x\n",
      "        print \"%6d\" %binom,\n",
      "    print \"\\n\"\n",
      "    q+=1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the number of rows: 9\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Pascal Triangle\n",
        "                                                                                     1 \n",
        "\n",
        "                                                                               1      1 \n",
        "\n",
        "                                                                         1      2      1 \n",
        "\n",
        "                                                                   1      3      3      1 \n",
        "\n",
        "                                                             1      4      6      4      1 \n",
        "\n",
        "                                                       1      5     10     10      5      1 \n",
        "\n",
        "                                                 1      6     15     20     15      6      1 \n",
        "\n",
        "                                           1      7     21     35     35     21      7      1 \n",
        "\n",
        "                                     1      8     28     56     70     56     28      8      1 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.11, page no. 136"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "print \"Enter 1 for triangle(3 sides) & 2 for triangle(base, ht): \",\n",
      "k = int(raw_input())\n",
      "if k == 1:\n",
      "    print \"Enter 3 sides - a,b and c\"\n",
      "    a = float(raw_input(\"a: \"))\n",
      "    b = float(raw_input(\"b: \"))\n",
      "    c = float(raw_input(\"c: \"))\n",
      "    p = a + b + c\n",
      "    s = (a + b + c)/2\n",
      "    area = math.sqrt(s*(s-a)*(s-b)*(s-c))\n",
      "    print \"Perimeter = %6.2f\" %p\n",
      "    print \"Area of triangle = %7.2f\" %area\n",
      "elif k == 2:\n",
      "    print \"Enter height and base\"\n",
      "    height = float(raw_input(\"height: \"))\n",
      "    base = float(raw_input(\"base: \"))\n",
      "    area = (base*height)/2\n",
      "    print \"Area of triangle = %7.2f\" %area"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter 1 for triangle(3 sides) & 2 for triangle(base, ht): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter 3 sides - a,b and c\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "a: 4.55\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b: 5.00\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c: 6.00\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Perimeter =  15.55\n",
        "Area of triangle =   11.11\n"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.12, page no. 137"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "print \"Enter the no of lines in the pyramid: \"\n",
      "t = int(raw_input())\n",
      "print \"\\t\\tREVERSED PYRAMID OF DIGITS\"\n",
      "k = 0\n",
      "for p in range(t, 0, -1):\n",
      "    k += 1\n",
      "    sys.stdout.write (\"   \")\n",
      "    for r in range(0, (k*4)+1):\n",
      "        sys.stdout.write (\" \")\n",
      "    for q in range(p, (2*p)-1):        \n",
      "        sys.stdout.write (\"%4d\" %q)\n",
      "    for s in range((2*p)-2, p-1, -1):\n",
      "        sys.stdout.write(\"%4d\\b\" %s)\n",
      "    sys.stdout.write (\"\\n\")\n",
      "print  \"                                 %d\" %p\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the no of lines in the pyramid: \n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\t\tREVERSED PYRAMID OF DIGITS\n",
        "           7   8   9  10  11  12  12\b  11\b  10\b   9\b   8\b   7\b\n",
        "               6   7   8   9  10  10\b   9\b   8\b   7\b   6\b\n",
        "                   5   6   7   8   8\b   7\b   6\b   5\b\n",
        "                       4   5   6   6\b   5\b   4\b\n",
        "                           3   4   4\b   3\b\n",
        "                               2   2\b\n",
        "                                \n",
        "                                 1\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.13, page no. 137"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "\n",
      "n = 20\n",
      "x = float(raw_input(\"Input x: \"))\n",
      "x = x*3.1412/180\n",
      "t = 1\n",
      "sum = 1\n",
      "for i in range(1, n+1):\n",
      "    t = t*pow(-1, (2*i-1))*x*x/(2*i*(2*i-1))\n",
      "    sum += t\n",
      "print \"COS(X) = %7.3f\" %sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input x: 90.00\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "COS(X) =   0.000\n"
       ]
      }
     ],
     "prompt_number": 62
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 4.14, page no. 138"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "n = int(raw_input(\"Enter the number of lines: \"))\n",
      "for p in range(1, n+1):\n",
      "    for i in range(1, n-p):\n",
      "        print \"     \",\n",
      "    m = p\n",
      "    for q in range(1, p+1):\n",
      "        print \"%4d\" %m,\n",
      "        m +=1\n",
      "    m = m - 2\n",
      "    for q in range(1, p):\n",
      "        print \"%4d\" %m,\n",
      "        m-=1\n",
      "    print \"\\n\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number of lines: 7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "                                 1 \n",
        "\n",
        "\n",
        "                           2    3    2 \n",
        "\n",
        "\n",
        "                     3    4    5    4    3 \n",
        "\n",
        "\n",
        "               4    5    6    7    6    5    4 \n",
        "\n",
        "\n",
        "         5    6    7    8    9    8    7    6    5 \n",
        "\n",
        "\n",
        "   6    7    8    9   10   11   10    9    8    7    6 \n",
        "\n",
        "\n",
        "   7    8    9   10   11   12   13   12   11   10    9    8    7 \n",
        "\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 67
    }
   ],
   "metadata": {}
  }
 ]
}