{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Loop Control Structures in C" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1, Page Number : LCS-2" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nEnter value to n : \"))\n", "\n", "for i in range(1,n+1):\n", " sys.stdout.write(\"%8d\"%(i))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to n : 15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, Page Number : LCS-3" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import numpy\n", "import math\n", "\n", "print \"\\n--------------------------------\"\n", "print \" X Y \"\n", "print \"--------------------------------\"\n", "\n", "for x in numpy.arange(1.0,3.0+0.2,0.2):\n", " y = 1.36*math.sqrt(1+x+x*x*x)+ pow(x,1.0/4) + math.exp(x)\n", " sys.stdout.write(\"\\n %6.2f %6.2f\"%(x,y))\n", " \n", "print \"\\n--------------------------------\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "--------------------------------\n", " X Y \n", "--------------------------------\n", "\n", " 1.00 6.07\n", " 1.20 7.06\n", " 1.40 8.23\n", " 1.60 9.60\n", " 1.80 11.20\n", " 2.00 13.09\n", " 2.20 15.30\n", " 2.40 17.91\n", " 2.60 20.99\n", " 2.80 24.64\n", " 3.00 28.97\n", "--------------------------------\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, Page Number : LCS-4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "k = int(raw_input(\"\\nEnter the number : \"))\n", "\n", "kfact = 1\n", "\n", "for i in range(1,k+1):\n", " kfact = kfact * i\n", " \n", "print k,\" factorial is \",kfact" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the number : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4 factorial is 24\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4, Page Number : LCS-6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter value to N : \"))\n", "\n", "s = 0\n", "\n", "for i in range(1,n+1):\n", " term = 0\n", " for j in range(1,i+1):\n", " term = term + j\n", " s = s + term\n", " \n", "print \"\\nSum of the series S = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to N : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of the series S = 20\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5, Page Number : LCS-7" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy\n", "import sys\n", "\n", "for x in numpy.arange(-1.5,1.5+0.5,0.5):\n", " for y in numpy.arange(0,3.0+1.0,1.0):\n", " z = 3*x*x + 2*y*y*y - 25.5\n", " sys.stdout.write(\"\\n\\nValue of y(%0.2f,%0.2f) = %6.2f\"%(x,y,z))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Value of y(-1.50,0.00) = -18.75\n", "\n", "Value of y(-1.50,1.00) = -16.75\n", "\n", "Value of y(-1.50,2.00) = -2.75\n", "\n", "Value of y(-1.50,3.00) = 35.25\n", "\n", "Value of y(-1.00,0.00) = -22.50\n", "\n", "Value of y(-1.00,1.00) = -20.50\n", "\n", "Value of y(-1.00,2.00) = -6.50\n", "\n", "Value of y(-1.00,3.00) = 31.50\n", "\n", "Value of y(-0.50,0.00) = -24.75\n", "\n", "Value of y(-0.50,1.00) = -22.75\n", "\n", "Value of y(-0.50,2.00) = -8.75\n", "\n", "Value of y(-0.50,3.00) = 29.25\n", "\n", "Value of y(0.00,0.00) = -25.50\n", "\n", "Value of y(0.00,1.00) = -23.50\n", "\n", "Value of y(0.00,2.00) = -9.50\n", "\n", "Value of y(0.00,3.00) = 28.50\n", "\n", "Value of y(0.50,0.00) = -24.75\n", "\n", "Value of y(0.50,1.00) = -22.75\n", "\n", "Value of y(0.50,2.00) = -8.75\n", "\n", "Value of y(0.50,3.00) = 29.25\n", "\n", "Value of y(1.00,0.00) = -22.50\n", "\n", "Value of y(1.00,1.00) = -20.50\n", "\n", "Value of y(1.00,2.00) = -6.50\n", "\n", "Value of y(1.00,3.00) = 31.50\n", "\n", "Value of y(1.50,0.00) = -18.75\n", "\n", "Value of y(1.50,1.00) = -16.75\n", "\n", "Value of y(1.50,2.00) = -2.75\n", "\n", "Value of y(1.50,3.00) = 35.25" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6, Page Number : LCS-12" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter value to N : \"))\n", "\n", "s = 0\n", "i = 1\n", "\n", "while i <= n:\n", " s = s + i\n", " i = i + 2\n", " \n", "print \"\\nSum of odd integers = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to N : 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of odd integers = 25\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, Page Number : LCS-13" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "print \"\\nInteger divisible by 7\"\n", "\n", "n = 7\n", "\n", "for i in range(1,50+1):\n", " sys.stdout.write(\"%8d\"%(n))\n", " n = n + 7" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Integer divisible by 7\n", " 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8, Page Number : LCS-14" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nEnter the end value N : \"))\n", "\n", "print \"\\nIntegers not divisible by 7\"\n", "\n", "for k in range(1,n+1):\n", " r = k % 7\n", " if r != 0:\n", " sys.stdout.write(\"%8d\"%(k))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the end value N : 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Integers not divisible by 7\n", " 1 2 3 4 5 6 8 9 10 11 12 13 15 16 17 18 19 20" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9, Page Number : LCS-15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter a positive integer : \"))\n", "\n", "q = n\n", "s = 0\n", "\n", "while q > 0:\n", " r = q % 10\n", " s = s + r\n", " q = q/ 10\n", " \n", "print \"\\nSum of digits = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a positive integer : 2466\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of digits = 18\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10, Page Number : LCS-16" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter an integer number : \"))\n", "\n", "q = n\n", "s = 0\n", "while q > 0:\n", " r = q % 10\n", " s = s + r*r*r\n", " q = q/10\n", " \n", "if n == s:\n", " print \"\\n\",n,\" is an Armstrong number\"\n", "else:\n", " print \"\\n\",n,\" is not an Armstrong number\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter an integer number : 153\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "153 is an Armstrong number\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11, Page Number : LCS-17" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter an integer number : \"))\n", "\n", "q = n\n", "rn = 0\n", "\n", "while q > 0:\n", " r = q % 10\n", " rn = rn*10 + r\n", " q = q/10\n", " \n", "print \"\\n\",n,\" is reversed as \",rn" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter an integer number : 18532\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "18532 is reversed as 23581\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 12, Page Number : LCS-18" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(raw_input(\"\\nEnter an integer number : \"))\n", "\n", "q = n\n", "rn = 0\n", "\n", "while q > 0:\n", " r = q % 10\n", " rn = rn*10 + r\n", " q = q/10\n", "\n", "while rn > 0:\n", " r = rn % 10\n", " \n", " #There is no switch statement in python\n", " if r == 1:\n", " print \"One\"\n", " else:\n", " if r == 2:\n", " print \"Two\"\n", " else:\n", " if r == 3:\n", " print \"Three\"\n", " else:\n", " if r == 4:\n", " print \"Four\"\n", " else:\n", " if r == 5:\n", " print \"Five\"\n", " else:\n", " if r == 6:\n", " print \"Six\"\n", " else:\n", " if r == 7:\n", " print \"Seven\"\n", " else:\n", " if r == 8:\n", " print \"Eight\"\n", " else:\n", " if r == 9:\n", " print \"Nine\"\n", " else:\n", " print \"Zero\"\n", " rn = rn/10" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter an integer number : 4352\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Four\n", "Three\n", "Five\n", "Two\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13, Page Number : LCS-20" ] }, { "cell_type": "code", "collapsed": false, "input": [ "flag = 0\n", "\n", "n = int(raw_input(\"\\nEnter a positive integer : \"))\n", "\n", "for k in range(2,n/2+1):\n", " if flag == 0:\n", " r = n % k\n", " if r == 0:\n", " flag = 1\n", " \n", "if flag == 0:\n", " print \"\\n\",n,\" is a prime number\"\n", "else:\n", " print \"\\n\",n,\" is not a prime number\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a positive integer : 17\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "17 is a prime number\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14, Page Number : LCS-21" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nEnter the final term of the series : \"))\n", "\n", "n1 = 0\n", "n2 = 1\n", "\n", "sys.stdout.write(\"%8d%8d\"%(n1,n2))\n", "\n", "newterm = n1 + n2\n", "\n", "while newterm <= n:\n", " sys.stdout.write(\"%8d\"%(newterm))\n", " n1 = n2\n", " n2 = newterm\n", " newterm = n1 + n2\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the final term of the series : 25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 0 1 1 2 3 5 8 13 21" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15, Page Number : LCS-22" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = float(raw_input(\"\\nEnter x in radians : \"))\n", "n = int(raw_input(\"\\nEnter end term power (n) : \"))\n", "\n", "s = 0\n", "term = x\n", "i = 1\n", "\n", "while i <= n:\n", " s = s + term\n", " term = (term*x*x*(-1))/((i+1)*(i+2))\n", " i = i + 2\n", " \n", "print \"\\nSum of the series = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter x in radians : 0.52\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter end term power (n) : 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of the series = 0.496880137863\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 16, Page Number : LCS-24" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = float(raw_input(\"\\nEnter x in radians : \"))\n", "\n", "s = 0\n", "term = 1\n", "i = 0\n", "\n", "while i <= n:\n", " s = s + term\n", " term = (term*x*x*(-1))/((i+1)*(i+2))\n", " i = i + 2\n", " \n", "print \"\\nSum of the series = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter x in radians : 0.52\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of the series = 0.867819179677\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17, Page Number : LCS-25" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import math\n", "\n", "s = 0\n", "dr = 1\n", "sign = 1\n", "term = 1.0/dr * sign\n", "\n", "while (math.fabs(term) >= 1.0e-4):\n", " s = s + term\n", " dr = dr + 2\n", " sign = sign * (-1)\n", " term = (1.0/dr) * sign\n", " \n", "pie = s * 4\n", "\n", "sys.stdout.write(\"\\nValue of pie is %0.2f\"%(pie))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Value of pie is 3.14" ] } ], "prompt_number": 27 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18, Page Number : LCS-27" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nEnter value to N : \"))\n", "\n", "s = 0\n", "\n", "for i in range(1,n+1):\n", " s = s + 1.0/i\n", " \n", "sys.stdout.write(\"\\nSum of series = %8.4f\"%(s))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to N : 15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of series = 3.3182" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 19, Page Number : LCS-28" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nHow many lines ? \"))\n", "\n", "for l in range(1,n+1):\n", " for i in range(1,n-l+1):\n", " sys.stdout.write(\" \")\n", " \n", " m = l\n", " for j in range(1,l+1):\n", " sys.stdout.write(\"%6d\"%(m))\n", " m = m + 1\n", " m = m - 2\n", " \n", " for k in range(1,l):\n", " sys.stdout.write(\"%6d\"%(m))\n", " m = m - 1\n", " \n", " sys.stdout.write(\"\\n\")\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many lines ? 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 1\n", " 2 3 2\n", " 3 4 5 4 3\n", " 4 5 6 7 6 5 4\n", " 5 6 7 8 9 8 7 6 5\n" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 20, Page Number : LCS-29" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "n = int(raw_input(\"\\nHow many rows ? \"))\n", "\n", "print \"\\n\\t\\t\\t\\t PASCAL TRIANGLE\\n\"\n", "\n", "m = 1\n", "\n", "for l in range(0,n):\n", " for i in range(40-3*l,0,-1):\n", " sys.stdout.write(\" \")\n", " \n", " for j in range(0,l+1):\n", " if j == 0 or l == 0:\n", " m = 1\n", " else:\n", " m = (m*(l-j+1))/j\n", " sys.stdout.write(\"%6d\"%(m))\n", " sys.stdout.write(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many rows ? 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t\t\t\t PASCAL TRIANGLE\n", "\n", " 1\n", " 1 1\n", " 1 2 1\n", " 1 3 3 1\n", " 1 4 6 4 1\n", " 1 5 10 10 5 1\n" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 21, Page Number : LCS-31" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "for i in range(1,5+1):\n", " sys.stdout.write(\"\\n\\nMultiplication table for %d\\n\"%(i))\n", " \n", " for j in range(1,10+1):\n", " sys.stdout.write(\"\\n%4d x %2d = %4d\"%(j,i,j*i))\n", " \n", " sys.stdout.write(\"\\n\\nPress any key to continue...\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Multiplication table for 1\n", "\n", " 1 x 1 = 1\n", " 2 x 1 = 2\n", " 3 x 1 = 3\n", " 4 x 1 = 4\n", " 5 x 1 = 5\n", " 6 x 1 = 6\n", " 7 x 1 = 7\n", " 8 x 1 = 8\n", " 9 x 1 = 9\n", " 10 x 1 = 10\n", "\n", "Press any key to continue...\n", "\n", "Multiplication table for 2\n", "\n", " 1 x 2 = 2\n", " 2 x 2 = 4\n", " 3 x 2 = 6\n", " 4 x 2 = 8\n", " 5 x 2 = 10\n", " 6 x 2 = 12\n", " 7 x 2 = 14\n", " 8 x 2 = 16\n", " 9 x 2 = 18\n", " 10 x 2 = 20\n", "\n", "Press any key to continue...\n", "\n", "Multiplication table for 3\n", "\n", " 1 x 3 = 3\n", " 2 x 3 = 6\n", " 3 x 3 = 9\n", " 4 x 3 = 12\n", " 5 x 3 = 15\n", " 6 x 3 = 18\n", " 7 x 3 = 21\n", " 8 x 3 = 24\n", " 9 x 3 = 27\n", " 10 x 3 = 30\n", "\n", "Press any key to continue...\n", "\n", "Multiplication table for 4\n", "\n", " 1 x 4 = 4\n", " 2 x 4 = 8\n", " 3 x 4 = 12\n", " 4 x 4 = 16\n", " 5 x 4 = 20\n", " 6 x 4 = 24\n", " 7 x 4 = 28\n", " 8 x 4 = 32\n", " 9 x 4 = 36\n", " 10 x 4 = 40\n", "\n", "Press any key to continue...\n", "\n", "Multiplication table for 5\n", "\n", " 1 x 5 = 5\n", " 2 x 5 = 10\n", " 3 x 5 = 15\n", " 4 x 5 = 20\n", " 5 x 5 = 25\n", " 6 x 5 = 30\n", " 7 x 5 = 35\n", " 8 x 5 = 40\n", " 9 x 5 = 45\n", " 10 x 5 = 50\n", "\n", "Press any key to continue..." ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 22, Page Number : LCS-32" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "n = int(raw_input(\"\\nEnter the binary number : \"))\n", "\n", "q = n\n", "s = 0\n", "k = 0\n", "\n", "while q > 0:\n", " r = q % 10\n", " s = s + r * pow(2,k)\n", " q = q/10\n", " k = k + 1\n", " \n", "print \"\\nThe decimal number is : \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the binary number : 1101\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The decimal number is : 13\n" ] } ], "prompt_number": 40 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13, Page Number : LCS-34" ] }, { "cell_type": "code", "collapsed": false, "input": [ "flag = 0\n", "k = 0\n", "\n", "n = int(raw_input(\"\\nEnter the decimal number : \"))\n", "\n", "q = n\n", "rbi = 0\n", "\n", "while q > 0:\n", " r = q % 2\n", " if r == 0 and flag == 0:\n", " k = k + 1\n", " else:\n", " flag = 1\n", " rbi = rbi *10 + r\n", " q = q/2\n", " \n", "q = rbi\n", "bi = 0\n", "\n", "while q > 0:\n", " r = q % 10\n", " bi = bi * 10 + r\n", " q = q/10\n", " if q == 0:\n", " for i in range(1,k+1):\n", " bi = bi * 10\n", " \n", "print \"\\nThe binary number is \",bi" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter the decimal number : 28\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The binary number is 11100\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }