summaryrefslogtreecommitdiff
path: root/Computer_Programming_Theory_and_Practice/Chapter5.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Computer_Programming_Theory_and_Practice/Chapter5.ipynb')
-rwxr-xr-xComputer_Programming_Theory_and_Practice/Chapter5.ipynb1103
1 files changed, 1103 insertions, 0 deletions
diff --git a/Computer_Programming_Theory_and_Practice/Chapter5.ipynb b/Computer_Programming_Theory_and_Practice/Chapter5.ipynb
new file mode 100755
index 00000000..52adbd63
--- /dev/null
+++ b/Computer_Programming_Theory_and_Practice/Chapter5.ipynb
@@ -0,0 +1,1103 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1c1423d8954dc10aa9bbb2818169dfeb793cb3bc9435852c4188cde598c54111"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5: Loop Control Structures in C"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1 , Page number: CP-79"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to print natural numbers from 1 to n\n",
+ "# Variable decalration\n",
+ "\n",
+ "n = 15\n",
+ "\n",
+ "print \"Enter value to n :\",n\n",
+ "\n",
+ "# Loop to print natural numbers\n",
+ "for r in range(15):\n",
+ " r = r + 1\n",
+ " print r,\n",
+ " \n",
+ " \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value to n : 15\n",
+ "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2 , Page number: CP-80"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to find the value of y and print a table for values of x\n",
+ "# Variable declaration\n",
+ "import math\n",
+ "x = 1.0\n",
+ "print \"------------------------\"\n",
+ "print \" x y \"\n",
+ "print \"------------------------\"\n",
+ "while x <= 3.2:\n",
+ " y = 1.36 * ((1 + x + x * x * x) ** 0.5) + ((x) ** (1.0/4)) + math.exp(x)\n",
+ " print \" %0.2f \" %x + \" %0.2f\" % y\n",
+ " x = x + 0.2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "------------------------\n",
+ " x y \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"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3 , Page number: CP-81"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to find factorial of given number\n",
+ "# Variable declaration\n",
+ "\n",
+ "k = 4\n",
+ "kfact = 1\n",
+ "\n",
+ "print \"Enter an integer :\",k\n",
+ "\n",
+ "# Loop to generate numbers from 1 to n\n",
+ "\n",
+ "for i in range(1,k + 1):\n",
+ " kfact = kfact*i\n",
+ "\n",
+ " \n",
+ "print \"4 factorial is\",kfact "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an integer : 4\n",
+ "4 factorial is 24\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4 , Page number: CP-83"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to print sum of the following series\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 4\n",
+ "s = 0\n",
+ "\n",
+ "print \"Enter value to N :\",n\n",
+ "# Calculation of sum\n",
+ "i = 1\n",
+ "j = 1\n",
+ "\n",
+ "for i in range(1,n+1):\n",
+ " term = 0\n",
+ " for j in range(i+1):\n",
+ " term = term + j\n",
+ " s = s + term\n",
+ "\n",
+ "\n",
+ "print \"Sum of the series S =\",s "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value to N : 4\n",
+ "Sum of the series S = 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5 , Page number: CP-84"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to compute the values of z based on x and y\n",
+ "# Variable declaration\n",
+ "\n",
+ "x = -1.5\n",
+ "y = 0\n",
+ "\n",
+ "# Loops to generate values of x and y to find z\n",
+ "\n",
+ "while x <= 1.5:\n",
+ " while y <= 3.0:\n",
+ " z = 3 * x * x + 2 * y * y * y - 25.5\n",
+ " print \"Value of y(\",x,\n",
+ " print \",\",y,\n",
+ " print \") =\",z\n",
+ " y = y + 1.0\n",
+ " x = x + 0.5\n",
+ " y = 0\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of y( -1.5 , 0 ) = -18.75\n",
+ "Value of y( -1.5 , 1.0 ) = -16.75\n",
+ "Value of y( -1.5 , 2.0 ) = -2.75\n",
+ "Value of y( -1.5 , 3.0 ) = 35.25\n",
+ "Value of y( -1.0 , 0 ) = -22.5\n",
+ "Value of y( -1.0 , 1.0 ) = -20.5\n",
+ "Value of y( -1.0 , 2.0 ) = -6.5\n",
+ "Value of y( -1.0 , 3.0 ) = 31.5\n",
+ "Value of y( -0.5 , 0 ) = -24.75\n",
+ "Value of y( -0.5 , 1.0 ) = -22.75\n",
+ "Value of y( -0.5 , 2.0 ) = -8.75\n",
+ "Value of y( -0.5 , 3.0 ) = 29.25\n",
+ "Value of y( 0.0 , 0 ) = -25.5\n",
+ "Value of y( 0.0 , 1.0 ) = -23.5\n",
+ "Value of y( 0.0 , 2.0 ) = -9.5\n",
+ "Value of y( 0.0 , 3.0 ) = 28.5\n",
+ "Value of y( 0.5 , 0 ) = -24.75\n",
+ "Value of y( 0.5 , 1.0 ) = -22.75\n",
+ "Value of y( 0.5 , 2.0 ) = -8.75\n",
+ "Value of y( 0.5 , 3.0 ) = 29.25\n",
+ "Value of y( 1.0 , 0 ) = -22.5\n",
+ "Value of y( 1.0 , 1.0 ) = -20.5\n",
+ "Value of y( 1.0 , 2.0 ) = -6.5\n",
+ "Value of y( 1.0 , 3.0 ) = 31.5\n",
+ "Value of y( 1.5 , 0 ) = -18.75\n",
+ "Value of y( 1.5 , 1.0 ) = -16.75\n",
+ "Value of y( 1.5 , 2.0 ) = -2.75\n",
+ "Value of y( 1.5 , 3.0 ) = 35.25\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6 , Page number: CP-89"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to find sum of odd integers between 1 to n\n",
+ "# Variable declararion\n",
+ "\n",
+ "n = 10\n",
+ "\n",
+ "print \"Enter value to N :\",n\n",
+ "\n",
+ "s = 0\n",
+ "i = 1\n",
+ "\n",
+ "while (i <= n):\n",
+ " s = s + i\n",
+ " i = i + 2\n",
+ "\n",
+ "print \"Sum of odd integers =\",s"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value to N : 10\n",
+ "Sum of odd integers = 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7 , Page number: CP-90"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to generate first 50 positive integers that are divisible by 7\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 7\n",
+ "print \"Integers divisible by 7\"\n",
+ "\n",
+ "#loop to print numbers divisible by 7\n",
+ "for n in range(7,353,7):\n",
+ " print n,\n",
+ " \n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integers 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\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8 , Page number: CP-91"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to print integers from 1 to n that are not divisible by 7\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 20\n",
+ "print \"Enter the end value N :\",n\n",
+ "\n",
+ "# Loop to print numbers that are not divisible by 7\n",
+ "\n",
+ "print \"Integers not divisible by 7\"\n",
+ "for k in range(1,n + 1,1):\n",
+ " r = k % 7\n",
+ " if (r != 0):\n",
+ " print k,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the end value N : 20\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\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9 , Page number: CP-92 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to print sum of digits of an integer\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 2466\n",
+ "\n",
+ "print \"Enter a positive integer :\",n\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",
+ "\n",
+ "print \"Sum of digits =\",s\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a positive integer : 2466\n",
+ "Sum of digits = 18\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10 , Page number: CP-93"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to check whether a given number is an armstrong number or not\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 153\n",
+ "q = n\n",
+ "s = 0\n",
+ "\n",
+ "print \"Enter an integer number :\",n\n",
+ "\n",
+ "# To check armstrong or not\n",
+ "\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 \"%d is an Armstrong number\" % n\n",
+ "else:\n",
+ " print \"%d is not an Armstrong number\" % n\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an integer number : 153\n",
+ "153 is an Armstrong number\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11 , Page number: CP-94"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to reverse a given integer\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 18532\n",
+ "q = n\n",
+ "rn = 0\n",
+ "\n",
+ "print \"Enter an integer number :\",n\n",
+ "# Reversing\n",
+ "\n",
+ "while (q > 0):\n",
+ " r = q % 10\n",
+ " rn = (rn * 10) + r\n",
+ " q = q / 10\n",
+ "\n",
+ "\n",
+ "print \"18532 is reversed as\",rn "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an integer number : 18532\n",
+ "18532 is reversed as 23581\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12 , Page number: CP-95"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to accept an integer and print digits using words\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 4352\n",
+ "q = n\n",
+ "rn = 0\n",
+ "\n",
+ "print \"Enter an integer number :\",n\n",
+ "# converting to digits\n",
+ "\n",
+ "while (q > 0):\n",
+ " r = q % 10\n",
+ " rn = rn * 10 + r\n",
+ " q = q / 10\n",
+ "\n",
+ "\n",
+ "while (rn > 0):\n",
+ " r = rn % 10\n",
+ " if (r == 1):\n",
+ " s = \"One\"\n",
+ " print s,\n",
+ " elif (r == 2):\n",
+ " s = \"Two\"\n",
+ " print s,\n",
+ " elif (r == 3):\n",
+ " s = \"Three\"\n",
+ " print s,\n",
+ " elif (r == 4):\n",
+ " s = \"Four\"\n",
+ " print s,\n",
+ " elif (r == 5):\n",
+ " s = \"Five\"\n",
+ " print s,\n",
+ " elif (r == 6):\n",
+ " s = \"Six\"\n",
+ " print s,\n",
+ " elif (r == 7):\n",
+ " s = \"Seven\"\n",
+ " print s,\n",
+ " elif (r == 8):\n",
+ " s = \"Eight\"\n",
+ " print s,\n",
+ " elif (r == 9):\n",
+ " s = \"Nine\"\n",
+ " print s,\n",
+ " elif (r == 0):\n",
+ " s = \"Zero\"\n",
+ " print s,\n",
+ " rn = rn / 10\n",
+ "\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an integer number : 4352\n",
+ "Four Three Five Two\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13 , Page number: CP-97"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to find whether a number is prime or not\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 17\n",
+ "\n",
+ "print \"Enter a positive integer :\",n\n",
+ "\n",
+ "# prime numbers are greater than 1\n",
+ "if n > 1:\n",
+ " # check for factors\n",
+ " for i in range(2,n):\n",
+ " if (n % i) == 0:\n",
+ " print(n,\"is not a prime number\")\n",
+ " print(i,\"times\",n//i,\"is\",num)\n",
+ " break\n",
+ " else:\n",
+ " print \"%d is a prime number\" % n\n",
+ "\n",
+ "else:\n",
+ " print \"%d is not a prime number\" % n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a positive integer : 17\n",
+ "17 is a prime number\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14 , Page number: CP-98"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to generate Fibonacci series\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 25\n",
+ "n1 = 0\n",
+ "n2 = 1\n",
+ "\n",
+ "print \"Enter the final term of the series :\",n\n",
+ "\n",
+ "print n1,n2,\n",
+ "\n",
+ "\n",
+ "newterm = n1 + n2\n",
+ "\n",
+ "# Loop to print fibonacci series\n",
+ "while (newterm <= n):\n",
+ " print newterm,\n",
+ " n1 = n2\n",
+ " n2 = newterm\n",
+ " newterm = n1 + n2\n",
+ "\n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the final term of the series : 25\n",
+ "0 1 1 2 3 5 8 13 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 15 , Page number: CP-99"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to solve the sine series\n",
+ "# Variable declaration\n",
+ "\n",
+ "x = 0.52\n",
+ "n = 10\n",
+ "s = 0\n",
+ "term = x\n",
+ "i = 1\n",
+ "\n",
+ "print \"Enter x in radians :\",x\n",
+ "print \"Enter end term power (n):\",n\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 \"Sum of the series = %0.6f\" % s\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter x in radians : 0.52\n",
+ "Enter end term power (n): 10\n",
+ "Sum of the series = 0.496880\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16 , Page number: CP-101"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to solve the cosine series\n",
+ "# Variable declaration\n",
+ "\n",
+ "x = 0.52\n",
+ "s = 0\n",
+ "term = 1\n",
+ "i = 0\n",
+ "n = 10\n",
+ "print \"Enter x in radians :\",x\n",
+ "\n",
+ "# Calculation of cosine series\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",
+ "\n",
+ "print \"Sum of the series = %0.6f\" % s "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter x in radians : 0.52\n",
+ "Sum of the series = 0.867819\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17 , Page number: CP-102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "# Program to compute the value of pie\n",
+ "# Variable declaration\n",
+ "\n",
+ "s = 0.0\n",
+ "dr = 1\n",
+ "sign = 1\n",
+ "term = (1.0/dr) * sign\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",
+ "print \"Value of pie is %.6f\"%pie"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of pie is 3.141393\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18 , Page number: CP-104"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to evaluate the series\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 15\n",
+ "s = 0.00\n",
+ "\n",
+ "print \"Enter value to N :\",n\n",
+ "\n",
+ "# Evaluation of series\n",
+ "\n",
+ "for i in range(1,n + 1,1):\n",
+ " s = float(s) + float(1.0 / i)\n",
+ " \n",
+ "\n",
+ "print \"Sum of series = %0.4f\" % s \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value to N : 15\n",
+ "Sum of series = 3.3182\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21 , Page number: CP-108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to print multiplication table\n",
+ "# Variable declaration\n",
+ "\n",
+ "i = 1\n",
+ "j = 1\n",
+ "\n",
+ "# nested loop to print multiplication tables\n",
+ "\n",
+ "for i in range(1,6):\n",
+ " print \"Multiplication table for\",i\n",
+ " for j in range(1,11):\n",
+ " print \" \",j,\"x\" , i ,\" =\" , j*i\n",
+ " print \"Press any key to continue. . .\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Multiplication table for 1\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",
+ "Press any key to continue. . .\n",
+ "Multiplication table for 2\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",
+ "Press any key to continue. . .\n",
+ "Multiplication table for 3\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",
+ "Press any key to continue. . .\n",
+ "Multiplication table for 4\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",
+ "Press any key to continue. . .\n",
+ "Multiplication table for 5\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",
+ "Press any key to continue. . .\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22 , Page number: CP-109"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "# Program to convert a binary number to a decimal number\n",
+ "# Variable declaration\n",
+ "\n",
+ "q = 1101\n",
+ "s = 0\n",
+ "k = 0\n",
+ "\n",
+ "print \"Enter the binary number :\",q\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",
+ "\n",
+ "print \"The decimal number is :\",s\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the binary number : 1101\n",
+ "The decimal number is : 13\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23 , Page number: CP-110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Program to convert a decimal number to a binary number\n",
+ "# Variable declaration\n",
+ "\n",
+ "n = 28\n",
+ "q = n\n",
+ "rbi = 0\n",
+ "flag = 0\n",
+ "k = 0\n",
+ "\n",
+ "print \"Enter the decimal number :\",n\n",
+ "\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",
+ "while (q > 0):\n",
+ " r = q % 10\n",
+ " bi = bi * 10 + r\n",
+ " q = q / 10\n",
+ " i = 1\n",
+ " if (q == 0):\n",
+ " while (i <= k):\n",
+ " bi = bi * 10\n",
+ " i = i + 1\n",
+ "\n",
+ "\n",
+ "print \"The binary number is \",bi "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the decimal number : 28\n",
+ "The binary number is 11100\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file