{
 "metadata": {
  "name": "",
  "signature": "sha256:dd103cfe67b47eb61e56816517923337936d461ec4fd4b8f9f87e6c544fd31e3"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 22 : Sample C Programs(Lab Exercise)"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 1: Page no.:L.4"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Area of Triangle\n",
      "\n",
      "import math\n",
      "\n",
      "#User Inputs\n",
      "a,b,c=input(\"Enter three sides: \")\n",
      "\n",
      "#calculation of semi perimeter\n",
      "s=(a+b+c)/2\n",
      "\n",
      "#Calculation of Area of Triangle\n",
      "d=(s*(s-a)*(s-b)*(s-c))\n",
      "\n",
      "area=math.sqrt(d)\n",
      "\n",
      "#Result\n",
      "print \"Area of triangle = %f sq units \\n\"%area"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter three sides: 5,6,7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Area of triangle = 14.696938 sq units \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 2: Page No. L.5"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Square root for the given value.\n",
      "\n",
      "import math\n",
      "\n",
      "#User Input\n",
      "x=input(\"Enter the Value: \")\n",
      "\n",
      "#Result\n",
      "print \"Square root of %.2f is: \"%(x)\n",
      "\n",
      "print math.sqrt(x)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Value: 81\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Square root of 81.00 is: \n",
        "9.0\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 3: Page.No.L.6"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Sum of the series 0+1+(1+2)+...+n\n",
      "\n",
      "#User Input\n",
      "n=input(\"Enter the Value of n : \")\n",
      "\n",
      "#Variable Declaration\n",
      "s=term=0\n",
      "i=j=0\n",
      "\n",
      "#Computing Values in loop\n",
      "for i in range(n):\n",
      "    for j in range(n):\n",
      "        if j<=i:\n",
      "            term=term+j\n",
      "            j=j+1        \n",
      "s=s+term\n",
      "if i<=n:\n",
      "    i=i+1\n",
      "    \n",
      "#Result\n",
      "print \"Sum of the series S= %d\"%(s)\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Value of n :5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum of the series S= 20\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 4: Page No.:L.9"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Decimal into binary number.\n",
      "\n",
      "n=input(\"Enter the decimal number: \")\n",
      "s=n\n",
      "k=[]\n",
      "\n",
      "#Calculation\n",
      "while (s>0):\n",
      "    a=int(float(s%2))\n",
      "    k.append(a)\n",
      "    s=(s-a)/2\n",
      "k.append(0)\n",
      "bi=\"\"\n",
      "\n",
      "for j in k[::1]:\n",
      "    bi=bi+str(j)\n",
      "\n",
      "#Result\n",
      "print('The binary value of %d is %s'%(n, bi))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the decimal number: 14\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The binary value of 14 is 01110\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 5: Page No. L.10"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#The given number is armstrong or not.\n",
      "\n",
      "#Variable declaration\n",
      "a=b=c=d=e=0\n",
      "\n",
      "#User Input\n",
      "a=input(\"Enter the Number: \")\n",
      "\n",
      "#Assigning variable 'a' value to variable 'e'\n",
      "e=a\n",
      "\n",
      "#Calculation for finding Armstrong or not\n",
      "while a>0:\n",
      "    b=a%10\n",
      "    c=b*b*b\n",
      "    a=a/10\n",
      "    d=c+d\n",
      " \n",
      "#Result\n",
      "if e==d:\n",
      "    print \"%d is an armstrong number. \"%e\n",
      "    \n",
      "else:\n",
      "    print \"%d is not an armstrong number. \"%e"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Number: 153\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "153 is an armstrong number. \n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 6: Page No.L.12"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Centigrade and Farenheit Values.\n",
      "\n",
      "\n",
      "#Variable Declaration\n",
      "c=f=cn=fn=0.0\n",
      "\n",
      "#User Input1 for calculating farenheit\n",
      "c=input(\"Enter temperature in centigrade : \")\n",
      "\n",
      "#Farenheit Calculation\n",
      "f=1.8*c+32\n",
      "\n",
      "#Result1\n",
      "print \"Farenheit Equivalent is: %.1f\\n\" %(f)\n",
      "\n",
      "#User input2 for calculating centigrade\n",
      "fn=input(\"Enter temperature in farenheit : \")\n",
      "\n",
      "#Centigrade Calculation\n",
      "cn=(fn-32)/1.8\n",
      "\n",
      "#Result2\n",
      "print \"Centigrade Equivalent is : %.1f\\n\"%(cn)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter temperature in centigrade : 20\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Farenheit Equivalent is: 68.0\n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter temperature in farenheit : 68\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Centigrade Equivalent is : 20.0\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 7: Page .no.L.13"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Converting Decimal value to binary number\n",
      "\n",
      "\n",
      "#Variable declaration\n",
      "bnum=digit=decimal=bina=base=0\n",
      "\n",
      "#User input\n",
      "bnum=input(\"Enter the binary number: \")\n",
      "\n",
      "bina=bnum\n",
      "\n",
      "while(bnum!=0):\n",
      "    digit=bnum%10\n",
      "    decimal=decimal+(digit<<base)\n",
      "    base=base+1\n",
      "    bnum=bnum/10\n",
      "    \n",
      "print \"The binary equivalent of %d in decimal = %d\"%(bina,decimal)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the binary number: 1111\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The binary equivalent of 1111 in decimal = 15\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 8: Page no.L.15"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Reverse number of the given number.\n",
      "\n",
      "#Function  Definition.\n",
      "def rev(n):\n",
      "    digit=rev_of=0\n",
      "    while n!=0:\n",
      "        digit=n%10\n",
      "        rev_of=rev_of*10+digit\n",
      "        n=n/10\n",
      "    return rev_of\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "\n",
      "#User input\n",
      "p=input(\"Enter the number: \")\n",
      "\n",
      "q=rev(p)\n",
      "\n",
      "print \"Reverse of %d is %d.\\n\"%(p,q)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number: 1234\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Reverse of 1234 is 4321.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 9: Page no.L.17"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Square of given value\n",
      "\n",
      "#Function definition\n",
      "def square(y):\n",
      "    return y*y\n",
      "\n",
      "#User input\n",
      "n=input(\"Enter the nth element : \")\n",
      "\n",
      "#Calculation\n",
      "for x in range(1,n+1):\n",
      "     print \"%d \" %(square(x))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the nth element : 8\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1 \n",
        "4 \n",
        "9 \n",
        "16 \n",
        "25 \n",
        "36 \n",
        "49 \n",
        "64 \n"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 10: Page no.:L.18"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Maximum value of given numbers\n",
      "\n",
      "#Function definition\n",
      "def maximum(x,y,z):\n",
      "    \n",
      "    maxi=x\n",
      "    if y>maxi:\n",
      "        maxi=y\n",
      "        if z>maxi:\n",
      "            maxi=z\n",
      "    return maxi\n",
      "    \n",
      "    \n",
      "#User input\n",
      "a,b,c=input(\"Enter three integers : \")\n",
      "\n",
      "#Result\n",
      "print \"Maximum is : %d\\n\"%(maximum(a,b,c))\n",
      "\n",
      "\n",
      "    \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter three integers : 2,6,9\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum is : 9\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 11: Page no.L.19"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Factorial of the given number using recursion.\n",
      "\n",
      "#funtion declaration\n",
      "def fact(n):\n",
      "    if n==0:\n",
      "        return 1\n",
      "    else:\n",
      "        return(n*fact(n-1))\n",
      "    \n",
      "#USer Input\n",
      "\n",
      "n=input(\"Enter the number whose factorial is to be found: \")\n",
      "\n",
      "#Result\n",
      "print \"The Factorial of %d is : %d \\n\"%(n,fact(n))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number whose factorial is to be found: 6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The Factorial of 6 is : 720 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 12: Page no.L.20"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Palindrome or not.\n",
      "\n",
      "print \"Enter the String to check palindrome or not \\n\"\n",
      "\n",
      "#User Input\n",
      "\n",
      "string = raw_input(\"Enter the String: \")\n",
      "\n",
      "#reverse the string \n",
      "\n",
      "rev_string = reversed(string)\n",
      "\n",
      "#Check if the string is palindrome or not\n",
      "\n",
      "if list(rev_string) == list(string) :\n",
      "    print \"\\nThe given string %s is a palindrome\"%(string)\n",
      "    \n",
      "else:\n",
      "    print \"\\nThe given string %s is not a palindrome\"%(string)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the String to check palindrome or not \n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the String: madam\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The given string madam is a palindrome\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 13: Page No. L.24"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Largest and smallest number in array\n",
      "\n",
      "#User input\n",
      "n= input(\"Enter the nth term of an array: \")\n",
      "\n",
      "for i in range(0,n):\n",
      "    a[i]=eval(raw_input())\n",
      "\n",
      "#check if the given array is large or small.    \n",
      "    for i in range(1,n):\n",
      "        if a[i]>large:\n",
      "           large=a[i]\n",
      "            \n",
      "        else:\n",
      "           if a[i]<small:\n",
      "              small=a[i]\n",
      "       \n",
      "         \n",
      "print \"Largest element in an array is: %.2f\"%large\n",
      "print \"Smallest element in an array is: %.2f\"%small\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the nth term of an array: 4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "12\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "79\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "50\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-21\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Largest element in an array is: 79.00\n",
        "Smallest element in an array is:-21.00\n"
       ]
      }
     ],
     "prompt_number": 40
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 14: Page no. L.26"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Given array in ascending order\n",
      "\n",
      "#User Input\n",
      "n=input(\"Enter the last term of the array: \")\n",
      "\n",
      "for i in range(0,n):\n",
      "    a[i]=eval(raw_input())\n",
      "for i in range(0,n-1):\n",
      "\n",
      "    for j in range(i+1,n):\n",
      "        if a[i]>a[j]:\n",
      "            temp=a[i]\n",
      "            a[i]=a[j]\n",
      "            a[j]=temp\n",
      "        \n",
      "print \"\\nThe Elements in ascending order\\n\"\n",
      "\n",
      "for i in range(0,n):\n",
      "    print \"%.2f\"%a[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the last term of the array: 6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "23\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "14\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-90\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-67\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "48\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "01\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "The Elements in ascending order\n",
        "\n",
        "-90.00\n",
        "-67.00\n",
        "1.00\n",
        "14.00\n",
        "23.00\n",
        "48.00\n"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 15: Page No. L.27"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Insert element in specified position in an array\n",
      "\n",
      "import array\n",
      "\n",
      "#User input\n",
      "n=input(\"Enter the last value: \")\n",
      "a=[0 for i in range(0,n) ]\n",
      "print \"Enter the elements of array: \"\n",
      "for i in range(0,n):\n",
      "    a[i]=eval(raw_input())\n",
      "    \n",
      "#Getting inserting element and position from users\n",
      "item=input(\"Enter the element to be inserted: \")\n",
      "pos=input(\"Enter the position of insertion: \")\n",
      "a.insert(pos,item)\n",
      "#Result\n",
      "print \"\\nElements after insertion is : \"\n",
      "for i in range(0,n+1):\n",
      "    print \"%.2f\"%a[i]\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the last value: 5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the elements of array: \n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "12\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "34\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "56\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "90\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the element to be inserted: 36\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the position of insertion: 4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements after insertion is : \n",
        "12.00\n",
        "34.00\n",
        "56.00\n",
        "90.00\n",
        "36.00\n",
        "32.00\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 16: Page.no.L.29"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Transpose of the matrix\n",
      "\n",
      "#User Input\n",
      "\n",
      "a=[]\n",
      "\n",
      "n,m=input(\"Enter the Row and Columns: \")\n",
      "print \"Enter the elements of matrix:\"\n",
      "for i in range(n):\n",
      "    a.append([])\n",
      "    \n",
      "    for j in range(m):\n",
      "        element=input()\n",
      "        a[i].append(element)\n",
      "\n",
      "#print matrix\n",
      "print \"\\nElements in matrix: \"\n",
      "for i in range(n):\n",
      "    for j in range(m):\n",
      "        print a[i][j],\n",
      "    print '\\n'\n",
      "\n",
      "\n",
      "#generate transpose\n",
      "transpose=[]\n",
      "for j in range(m):\n",
      "    transpose.append([])\n",
      "    for i in range (n):\n",
      "        b=a[i][j]\n",
      "        transpose[j].append(b)\n",
      "\n",
      "#print transpose\n",
      "print \"Transpose of the matrix is: \"\n",
      "for i in range (m):\n",
      "    for j in range (n):\n",
      "        print transpose[i][j],\n",
      "    print '\\n'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns: 3,3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the elements of matrix:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements in matrix: \n",
        "1 2 3 \n",
        "\n",
        "4 5 6 \n",
        "\n",
        "7 8 9 \n",
        "\n",
        "Transpose of the matrix is: \n",
        "1 4 7 \n",
        "\n",
        "2 5 8 \n",
        "\n",
        "3 6 9 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 52
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 17: Page no. L.32"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Program to find sum of an array matrix\n",
      "#Array Declaration\n",
      "\n",
      "a=[]\n",
      "b=[]\n",
      "c=[[0 for i in xrange(0,5)] for i in xrange(0,5)]\n",
      "#User Input1\n",
      "n,m=input(\"Enter the Row and Columns of A Matrix: \")\n",
      "print \"Enter the elements of A matrix:\"\n",
      "for i in range(n):\n",
      "    a.append([])\n",
      "    \n",
      "    for j in range(m):\n",
      "        element=input()\n",
      "        a[i].append(element)\n",
      "\n",
      "#print matrix1\n",
      "print \"\\nElements in A matrix: \"\n",
      "for i in range(n):\n",
      "    for j in range(m):\n",
      "        print a[i][j],\n",
      "    print '\\n'\n",
      "\n",
      "    \n",
      "#User Input 2\n",
      "p,q=input(\"Enter the Row and Columns of B Matrix: \")\n",
      "print \"Enter the elements of B matrix:\"\n",
      "for i in range(p):\n",
      "    b.append([])\n",
      "    \n",
      "    for j in range(q):\n",
      "        element=input()\n",
      "        b[i].append(element)\n",
      "\n",
      "#print matrix 2\n",
      "print \"\\nElements in B matrix: \"\n",
      "for i in range(p):\n",
      "    for j in range(q):\n",
      "        print b[i][j],\n",
      "    print '\\n'\n",
      "\n",
      "#Addition of array in matrix    \n",
      "for i in range(n):\n",
      "    for j in range(m):\n",
      "        c[i][j] += a[i][j] + b[i][j]\n",
      "\n",
      "#Result\n",
      "print \"\\nThe Sum of A and B matrix: \"\n",
      "for i in range(n):\n",
      "    for j in range(m):\n",
      "        print \"%5d\"%c[i][j],\n",
      "    print '\\n'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of A Matrix: 3,3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the elements of A matrix:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements in A matrix: \n",
        "1 2 3 \n",
        "\n",
        "4 5 6 \n",
        "\n",
        "7 8 9 \n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of B Matrix: 3,3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the elements of B matrix:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements in B matrix: \n",
        "1 2 3 \n",
        "\n",
        "4 5 6 \n",
        "\n",
        "6 7 4 \n",
        "\n",
        "\n",
        "The Sum of A and B matrix: \n",
        "    2     4     6 \n",
        "\n",
        "    8    10    12 \n",
        "\n",
        "   13    15    13 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 80
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 18: Page No. :L.35"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Program to find Matrix Multiplication \n",
      "#Array Declaration\n",
      "\n",
      "a=[]\n",
      "b=[]\n",
      "c=[[0 for i in xrange(0,5)] for i in xrange(0,5)]\n",
      "\n",
      "while True:\n",
      "#User Input1\n",
      "    n,m=input(\"Enter the Row and Columns of A Matrix: \")\n",
      "    p,q=input(\"Enter the Row and Columns of B Matrix: \")\n",
      "    if (n==m&p==q):\n",
      "        print \"Enter the elements of A matrix:\"   \n",
      "        for i in range(n):\n",
      "            a.append([]) \n",
      "            for j in range(m):\n",
      "                element=input()\n",
      "                a[i].append(element)\n",
      "#print matrix1\n",
      "        print \"\\nElements in A matrix: \"\n",
      "        for i in range(n):\n",
      "            for j in range(m):\n",
      "                print a[i][j],\n",
      "            print '\\n'\n",
      "   \n",
      "#User Input 2    \n",
      "        print \"Enter the elements of B matrix:\"\n",
      "        for i in range(p):\n",
      "            b.append([])\n",
      "            for j in range(q):\n",
      "                element=input()\n",
      "                b[i].append(element)\n",
      "\n",
      "#print matrix 2\n",
      "        print \"\\nElements in B matrix: \"\n",
      "        for i in range(p):\n",
      "            for j in range(q):\n",
      "                print b[i][j],\n",
      "            print '\\n'\n",
      "\n",
      "#Multiplication of array in matrix    \n",
      "        for i in range(0,m):\n",
      "            for j in range(0,n):\n",
      "                c[i][j] = 0\n",
      "                for k in range(0,n):\n",
      "                    c[i][j] += a[i][k] * b[k][j]\n",
      "        \n",
      "#Result\n",
      "        print \"\\nThe Multiplication of A and B matrix: \"\n",
      "        for i in range(n):\n",
      "            for j in range(m):\n",
      "                print \"%5d\"%c[i][j],\n",
      "            print '\\n'\n",
      "        break\n",
      "    else:\n",
      "        print \"The No. of rows and columns are not equal.\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of A Matrix: 3,3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of B Matrix: 2,2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The No. of rows and columns are not equal.\n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of A Matrix: 3,3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Row and Columns of B Matrix: 3,3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the elements of A matrix:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "9\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements in A matrix: \n",
        "1 2 3 \n",
        "\n",
        "4 5 6 \n",
        "\n",
        "7 8 9 \n",
        "\n",
        "Enter the elements of B matrix:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Elements in B matrix: \n",
        "2 4 1 \n",
        "\n",
        "6 7 4 \n",
        "\n",
        "3 5 7 \n",
        "\n",
        "\n",
        "The Multiplication of A and B matrix: \n",
        "   23    33    30 \n",
        "\n",
        "   56    81    66 \n",
        "\n",
        "   89   129   102 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 93
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 19: Page No. L.38"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Student grade using structure\n",
      "\n",
      "#User Input\n",
      "n=input(\"Enter the number of students: \")\n",
      "\n",
      "#Defining Structure\n",
      "class std:\n",
      "    no=0\n",
      "    name = ''\n",
      "    m1 = m2 = m3 =0\n",
      "    total=avg=0.0\n",
      "    grade= ''\n",
      "    \n",
      "s=[std() for i in range(0,n)]\n",
      "\n",
      "#User Input\n",
      "for i in range(0,n):\n",
      "    print \"\\nEnter the student %d details\"%(i+1)\n",
      "    print \"\\n\"\n",
      "    s[i].no=int(input(\"Give Student Number: \"))\n",
      "    s[i].name=raw_input(\"Enter the Student Name: \")\n",
      "    s[i].m1=int(input(\"Enter Student Mark1: \"))\n",
      "    s[i].m2=int(input(\"Enter Student Mark2: \"))\n",
      "    s[i].m3=int(input(\"Enter Student Mark3: \"))\n",
      "\n",
      "\n",
      "#Calculation    \n",
      "    s[i].total = (s[i].m1 + s[i].m2 + s[i].m3)\n",
      "    s[i].avg=(s[i].total / 3)\n",
      "    if s[i].avg<=40:\n",
      "        s[i].grade='D'\n",
      "    elif s[i].avg<60:\n",
      "        s[i].grade='C'\n",
      "    elif s[i].avg<80:\n",
      "        s[i].grade='B'\n",
      "    else:\n",
      "        s[i].grade='A'\n",
      "        \n",
      "#Result\n",
      "print \"\\tStudent mark details.... \\n\"\n",
      "print \"Roll No\\t Name \\t\\tMark1 \\tMark2 \\tMark3 \\tTotal \\tAverage \\tGrade\"\n",
      "for i in range(0,n):\n",
      "    print \"%d\\t\"%(s[i].no),\"%s\\t\\t\"%(s[i].name), \"%d\\t\"%(s[i].m1),\"%d\\t\"%(s[i].m2),\"%d\\t\"%(s[i].m3), \"%.2f\\t\"%(s[i].total), \"%.2f\\t\\t\"%(s[i].avg), \"%s\\t\"%(s[i].grade)\n",
      "   "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number of students: 2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the student 1 details\n",
        "\n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Give Student Number: 100\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Student Name: Venkat\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark1: 89\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark2: 87\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark3: 76\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter the student 2 details\n",
        "\n",
        "\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Give Student Number: 101\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Student Name: Shankar\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark1: 87\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark2: 90\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Student Mark3: 67\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\tStudent mark details.... \n",
        "\n",
        "Roll No\t Name \t\tMark1 \tMark2 \tMark3 \tTotal \tAverage \tGrade\n",
        "100\tVenkat\t\t89\t87\t76\t252.00\t84.00\t\tA\t\n",
        "101\tShankar\t\t87\t90\t67\t244.00\t81.00\t\tA\t\n"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 20: Page No. L.40"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Employee details using Union\n",
      "\n",
      "#Defining Union\n",
      "\n",
      "class union:\n",
      "    name=\"\"\n",
      "    idno=0\n",
      "    salary=0.0\n",
      "    \n",
      "desc=union()\n",
      "\n",
      "desc.name=\"vinod\"\n",
      "\n",
      "print \"Employee details:\"\n",
      "\n",
      "print \"The name is %s\\n\"%(desc.name)\n",
      "print \"The idno is %d\\n\"%(desc.idno)\n",
      "print \"The salary is %6.2f\\n\"%(desc.salary)\n",
      "\n",
      "desc.idno=10\n",
      "\n",
      "print \"Employe details:\"\n",
      "\n",
      "print \"The name is %s\\n\"%(desc.name)\n",
      "print \"The idno is %d\\n\"%(desc.idno)\n",
      "print \"The salary is %6.2f\\n\"%(desc.salary)\n",
      "\n",
      "desc.salary=6500.00\n",
      "\n",
      "print \"Employee details:\"\n",
      "\n",
      "print \"The name is %s\\n\"%(desc.name)\n",
      "print \"The idno is %d\\n\"%(desc.idno)\n",
      "print \"The salary is %6.2f\\n\"%(desc.salary)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Employee details:\n",
        "The name is vinod\n",
        "\n",
        "The idno is 0\n",
        "\n",
        "The salary is   0.00\n",
        "\n",
        "Employe details:\n",
        "The name is vinod\n",
        "\n",
        "The idno is 10\n",
        "\n",
        "The salary is   0.00\n",
        "\n",
        "Employee details:\n",
        "The name is vinod\n",
        "\n",
        "The idno is 10\n",
        "\n",
        "The salary is 6500.00\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 58
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 21: Page No. L.41"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Swap the contents of two variable using pointer.\n",
      "\n",
      "#swapping function.\n",
      "\n",
      "def interchange(x,y):\n",
      "    temp=x\n",
      "    x=y\n",
      "    y=temp\n",
      "    return x,y\n",
      "\n",
      "#User Input\n",
      "a,b = input(\"Enter the values of A and B: \")\n",
      "#Result before interchanging\n",
      "print \"a=%d b=%d\\n\"%(a,b)\n",
      "\n",
      "\n",
      "#Result after interchanging\n",
      "print \"After interchanging: \\n\"\n",
      "\n",
      "print \"a=%d b=%d\\n\"%(interchange(a,b))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the values of A and B: 23,56\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "a=23 b=56\n",
        "\n",
        "After interchanging: \n",
        "\n",
        "a=56 b=23\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 66
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 22: Page No.: L.43"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Concatenate two strings using Dynamic Memory Allocation \n",
      "\n",
      "s1= raw_input(\"Enter String One: \")\n",
      "s2=raw_input(\"Enter String two: \")\n",
      "\n",
      "s3=''\n",
      "s3=s1+s2                    \n",
      "#Result\n",
      "print \"Concatenated String: \", s1+s2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter String One: venkat\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter String two: raman\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Concatenated String:  venkatraman\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 23: Page No. L.44"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#program to store and print same data to and from the file.\n",
      "\n",
      "#open file\n",
      "fp=open(\"Text.txt\",\"w\")\n",
      "\n",
      "#User Input\n",
      "c=raw_input(\"Enter the Text: \")\n",
      "\n",
      "#Store given input into a file \"text.txt\"\n",
      "fp.write(c)\n",
      "\n",
      "#Closing file\n",
      "fp.close()\n",
      "\n",
      "#Again open text.txt\n",
      "fp=open(\"Text.txt\", \"r\")\n",
      "\n",
      "#display the content from the file \"text.txt\"\n",
      "print \"Displaying text from file:\\n\",c \n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Text: VRB PUBLISHERS\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Displaying text from file:\n",
        "VRB PUBLISHERS\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 24: Page No. L.46"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Program to count number of characters in a file.\n",
      "\n",
      "#create a file\n",
      "fp=open(\"str.txt\",\"w\")\n",
      "c=\"\"\n",
      "print \"Enter the Characters:\"\n",
      "print \"Press ctrl+z to stop entry\"\n",
      "\n",
      "#User Input\n",
      "c=raw_input()\n",
      "#store input into the file\n",
      "fp.write(c)\n",
      "fp.close()\n",
      "\n",
      "#Again open the file to read\n",
      "fp=open(\"str.txt\",\"r\")\n",
      "\n",
      "#Result\n",
      "print \"\\n%s\"%(c)\n",
      "print \"Number of characters in the file: \",len(c)\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the Characters:\n",
        "Press ctrl+z to stop entry\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "vrb publishers private limited\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "vrb publishers private limited\n",
        "Number of characters in the file:  30\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercise 25: Page No.: L.48"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Program using Memory Allocation function\n",
      "\n",
      "NULL=0\n",
      "buffer_size=10\n",
      "\n",
      "buffer1 = \"BOMBAY\"\n",
      "print \"Buffer of size %d created\"%(buffer_size)\n",
      "print \"Buffer contains\", buffer1\n",
      "    \n",
      "\n",
      "if buffer_size==NULL:\n",
      "    print \"Malloc failed\"\n",
      "    \n",
      "else:\n",
      "    print \"Buffer size modified\"\n",
      "    print \"Buffer still contains:\", buffer1\n",
      "    \n",
      "buffer1=\"MUMBAI\"\n",
      "print \"Buffer now contains:\", buffer1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Buffer of size 10 created\n",
        "Buffer contains BOMBAY\n",
        "Buffer size modified\n",
        "Buffer still contains: BOMBAY\n",
        "Buffer now contains: MUMBAI\n"
       ]
      }
     ],
     "prompt_number": 19
    }
   ],
   "metadata": {}
  }
 ]
}