diff options
author | hardythe1 | 2015-04-07 15:58:05 +0530 |
---|---|---|
committer | hardythe1 | 2015-04-07 15:58:05 +0530 |
commit | c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131 (patch) | |
tree | 725a7d43dc1687edf95bc36d39bebc3000f1de8f /Computer_Programming_Theory_and_Practice | |
parent | 62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 (diff) | |
download | Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.gz Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.bz2 Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.zip |
added books
Diffstat (limited to 'Computer_Programming_Theory_and_Practice')
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter11.ipynb | 62 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter2.ipynb | 41 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter3.ipynb | 293 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter4.ipynb | 524 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter5.ipynb | 1103 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter6.ipynb | 794 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter7.ipynb | 557 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter8.ipynb | 1027 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/Chapter9.ipynb | 624 | ||||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/screenshots/chapter11.png | bin | 0 -> 169980 bytes | |||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/screenshots/chapter2.png | bin | 0 -> 147743 bytes | |||
-rwxr-xr-x | Computer_Programming_Theory_and_Practice/screenshots/chapter3.png | bin | 0 -> 154011 bytes |
12 files changed, 5025 insertions, 0 deletions
diff --git a/Computer_Programming_Theory_and_Practice/Chapter11.ipynb b/Computer_Programming_Theory_and_Practice/Chapter11.ipynb new file mode 100755 index 00000000..c046ab93 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter11.ipynb @@ -0,0 +1,62 @@ +{ + "metadata": { + "name": "Chapter11" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Chapter 11, Files" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example 1, Page Number:CP-277" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Program to create a text file\n\nfp = open(\"sample.txt\",\"w\") # open file in write mode\n\nprint \"Type the text and Press enter key at end.\"\nprint\nprint \"Computer Programming in C language is widely used for Science and Engineering applications\"\n# input data to file\nch = ['Computer ','Programming ','in ','C ','language ','is ','widely ','used ','for ','Science ','and ' ,'Engineering ','applications.']\nfor i in ch:\n fp.write(i)\nfp.close()\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Type the text and Press enter key at end.\n\nComputer Programming in C language is widely used for Science and Engineering applications\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example 2, Page Number:CP-278" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# Program to read text file and count number of vowels\n\nfp = open(\"sample.txt\",\"r\") # open file in read mode\ncount = 0\nprint \"The Content of the file is:\"\n\nwhile (1): \n ch = fp.read(1)\n if not ch:\n break\n print ch\n # switch statements\n if ch == \"A\" or ch == \"a\": # case 'A' or 'a'\n count = count + 1\n elif ch == \"E\" or ch == \"e\": # case 'E' or 'e'\n count = count + 1\n elif ch == \"I\" or ch == \"i\": # case 'I' or 'i'\n count = count + 1\n elif ch == \"O\" or ch == \"o\": # case 'O' or 'o'\n count = count + 1\n elif ch == \"U\" or ch == \"u\": # case 'U' or 'u'\n count = count + 1\n\n\n\nfp.close() \nprint \"Number of vowels present =\",count \n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Content of the file is:\nC\no\nm\np\nu\nt\ne\nr\n \nP\nr\no\ng\nr\na\nm\nm\ni\nn\ng\n \ni\nn\n \nC\n \nl\na\nn\ng\nu\na\ng\ne\n \ni\ns\n \nw\ni\nd\ne\nl\ny\n \nu\ns\ne\nd\n \nf\no\nr\n \nS\nc\ni\ne\nn\nc\ne\n \na\nn\nd\n \nE\nn\ng\ni\nn\ne\ne\nr\ni\nn\ng\n \na\np\np\nl\ni\nc\na\nt\ni\no\nn\ns\n.\nNumber of vowels present = 31\n" + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter2.ipynb b/Computer_Programming_Theory_and_Practice/Chapter2.ipynb new file mode 100755 index 00000000..c4bc71ff --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter2.ipynb @@ -0,0 +1,41 @@ +{ + "metadata": { + "name": "Chapter2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Chapter 2, Fundamentals of C language" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example 3, Page Number: CP-22" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "# variable declaration\n\na = 5\nb = 7\nc = 2\n\n# calculation \n\ny = a + b / c - a % c + 2 * a\n\n# result \n\nprint \"The result is \",y\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The result is 17\n" + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter3.ipynb b/Computer_Programming_Theory_and_Practice/Chapter3.ipynb new file mode 100755 index 00000000..04d24871 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter3.ipynb @@ -0,0 +1,293 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:0ff7bb47f00bae9dc5a49237df7011debd17443ba836341d55e8fe830338ade7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3: Input/Output Functions and Statements" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#program to add and find product of two numbers\n", + "\n", + "#variable declaration\n", + "a = 5\n", + "b = 3\n", + "\n", + "# calculation of sum and product \n", + "\n", + "summ = a + b\n", + "product = a * b\n", + "\n", + "print a,b\n", + "print summ,product" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 3\n", + "8 15\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to convert degree fahrenheit to celcius \n", + "# variable declaration\n", + "\n", + "f = 105.00\n", + "print \"Degree fahrenheit ? %d\" % f\n", + "# calculation of degree in celcius \n", + "\n", + "c = 5.0/9.0 * (f-32)\n", + "\n", + "# result \n", + "print\n", + "print \"Degree centigrade =%6.2f\" % c" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Degree fahrenheit ? 105\n", + "\n", + "Degree centigrade = 40.56\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: CP-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# To find area of a triangle\n", + "# variable declaration\n", + "\n", + "a = 5\n", + "b = 4\n", + "c = 6\n", + "\n", + "\n", + "# calculation of area\n", + "s = float((a+b+c))/2\n", + "area = (s*(s-a)*(s-b)*(s-c)) ** 0.5\n", + "\n", + "# result\n", + "print \"Enter three sides : %d\" % a,b,c\n", + "print \n", + "print \"Area of triangle = %0.2f Sq.units\" % area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three sides : 5 4 6\n", + "\n", + "Area of triangle = 9.92 Sq.units\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# To print ASCII value of a given character \n", + "# Variable declaration\n", + "\n", + "ch = \"A\"\n", + "print \"Enter a character : \" , ch\n", + "\n", + "# Calculation of ASCII value of a character\n", + "\n", + "print \n", + "print \"ASCII value of \" + ch + \" is\" ,ord(ch)\n", + "print \"Press any key to stop. . .\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a character : A\n", + "\n", + "ASCII value of A is 65\n", + "Press any key to stop. . .\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# To print electricity for consumers\n", + "# Variable declaration\n", + "\n", + "sno = \"TMR65358\"\n", + "pmr = 4305\n", + "cmr = 4410\n", + "\n", + "print \"Enter service number :\" ,sno\n", + "print \"Previous meter reading ?\",pmr\n", + "print \"Current meter reading ?\",cmr\n", + "\n", + "# Calculation of electricity charges\n", + "\n", + "units = cmr - pmr\n", + "amt = units * 1.50\n", + "\n", + "# Result\n", + "\n", + "print\n", + "print \" Electricity Bill\"\n", + "print \" ----------------\"\n", + "print \"Service No :\",sno\n", + "print \"Unit Consumed :\",units\n", + "print \"Electricity Charges : Rs.%0.2f\" % amt" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter service number : TMR65358\n", + "Previous meter reading ? 4305\n", + "Current meter reading ? 4410\n", + "\n", + " Electricity Bill\n", + " ----------------\n", + "Service No : TMR65358\n", + "Unit Consumed : 105\n", + "Electricity Charges : Rs.157.50\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-52" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to swap value of two variables\n", + "# Variable declaration\n", + "\n", + "a = 15\n", + "b = 250 \n", + "\n", + "print \"Enter value to A :\",a\n", + "print \"Enter value to B :\",b\n", + "\n", + "# Swapping\n", + "\n", + "temp = a\n", + "a = b\n", + "b = temp\n", + "\n", + "print \n", + "print \"Value of A =\",a\n", + "print \"Value of B =\",b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to A : 15\n", + "Enter value to B : 250\n", + "\n", + "Value of A = 250\n", + "Value of B = 15\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter4.ipynb b/Computer_Programming_Theory_and_Practice/Chapter4.ipynb new file mode 100755 index 00000000..368164f3 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter4.ipynb @@ -0,0 +1,524 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c9e723d7feeae217ee8ddaf1b9cf8cfab2d696821fa911ff65107b9da6bec9f0" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Control Statements in C " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-60" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the biggest of two numbers\n", + "# Variable declaration\n", + "\n", + "a = 5\n", + "b = 8\n", + "\n", + "print \"Enter two numbers : %d\" % a,b\n", + "\n", + "# Calculation\n", + "\n", + "big = a\n", + "if (b>big):\n", + " big = b\n", + "\n", + "print \"Biggest number is \",big" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two numbers : 5 8\n", + "Biggest number is 8\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-61" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find biggest of three numbers\n", + "# Variable declaration\n", + "\n", + "a = 5\n", + "b = 13\n", + "c = 8\n", + "\n", + "print \"Enter three numbers : %d\" % a,b,c\n", + "\n", + "# Calculate\n", + "\n", + "big = a\n", + "if (b>big):\n", + " big = b\n", + "if (c>big):\n", + " big = c\n", + "\n", + "print \"Biggest number is\",big" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three numbers : 5 13 8\n", + "Biggest number is 13\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: CP-63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find biggest of three numbers\n", + "# Variable declaration\n", + "\n", + "a = 18\n", + "b = -5\n", + "c = 13\n", + " \n", + "print \"Enter three numbers : %d\" % a,b,c\n", + "\n", + "# Calculation to find biggest number\n", + "\n", + "if (a>b):\n", + " if(a>c):\n", + " big = a\n", + " else:\n", + " big = c\n", + "else:\n", + " if(b>c):\n", + " big = b\n", + " else:\n", + " big = c\n", + "\n", + "print \"Biggest number is\",big" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three numbers : 18 -5 13\n", + "Biggest number is 18\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-64" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the value of y \n", + "# Variable declration\n", + "\n", + "x = 0.42\n", + "n = 5\n", + "\n", + "print \"Enter value to x and n :\" , x,n \n", + "\n", + "# Calculation\n", + "\n", + "if (n==1):\n", + " y = 1 + x\n", + "elif (n==2):\n", + " y = 1 + x / n\n", + "elif (n==3):\n", + " y = 1 + (x ** n)\n", + "else:\n", + " y = 1 + n * x\n", + "\n", + "\n", + "print \"Value of y(x,n) = %0.2f\" % y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to x and n : 0.42 5\n", + "Value of y(x,n) = 3.10\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the value of y\n", + "# Variable declaration\n", + "\n", + "x = 0.42\n", + "n = 5\n", + "\n", + "\n", + "print \"Enter value to x and n :\", x,n\n", + "\n", + "# Calculation\n", + "\n", + "# Switch case statements \n", + "if n == 1: # case 1\n", + " y = 1 + x\n", + "elif n == 2: # case 2\n", + " y = 1 + x / n\n", + "elif n == 3: # case 3\n", + " y = 1 + (x ** n)\n", + "else: # default\n", + " y = 1 + n * x\n", + "\n", + "print \"Value of y(x,n) = %0.2f\" % y " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to x and n : 0.42 5\n", + "Value of y(x,n) = 3.10\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to caculate the commission for sales representatives\n", + "# Variable declaration\n", + "\n", + "sales = 4500\n", + "\n", + "print \"Sales amount ? :\" , sales\n", + "\n", + "# Calculation of commission\n", + "\n", + "if (sales <= 500):\n", + " comm = 0.05 * sales\n", + "elif (sales <= 2000):\n", + " comm = 35 + 0.10 * (sales - 500)\n", + "elif (sales <= 5000):\n", + " comm = 185 + 0.12 * (sales - 2000)\n", + "else:\n", + " comm = 0.125 * sales\n", + "\n", + "\n", + "print \"Commission Amount Rs.%0.2f\" % comm" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sales amount ? : 4500\n", + "Commission Amount Rs.485.00\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7 , Page number: CP-69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find roots of a quadratic equation\n", + "# Variable declaration\n", + "\n", + "a = 1\n", + "b = 3\n", + "c = 2\n", + "\n", + "print \"Enter coefficients a, b, and c :\", a,b,c\n", + "\n", + "d = b * b - 4 * a * c\n", + "\n", + "# Calculation of roots\n", + "\n", + "if (d > 0):\n", + " x1 = (-b + (d ** 0.5)) / (2 * a)\n", + " x2 = (-b - (d ** 0.5)) / (2 * a)\n", + " print \"Roots are real and unequal \"\n", + " print x1,x2\n", + " \n", + "\n", + "elif(d == 0):\n", + " x = -b / (2 * a)\n", + " print \"Roots are real and equal\"\n", + " print \"%6.2f\" % x\n", + "\n", + "else:\n", + " print \"No Real roots, roots are complex\"\n", + "\n", + "\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter coefficients a, b, and c : 1 3 2\n", + "Roots are real and unequal \n", + "-1.0 -2.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8 , Page number: CP-70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to print grade\n", + "# Variable declaration\n", + "\n", + "avg_marks = 84\n", + "\n", + "print \"Average marks ?\",avg_marks\n", + "\n", + "# Calculation of grade\n", + "\n", + "if (avg_marks >= 80) and (avg_marks <= 100):\n", + " print \"Honours\"\n", + "elif (avg_marks >= 60) and (avg_marks <=79):\n", + " print \"First Division\"\n", + "elif (avg_marks >= 50) and (avg_marks <= 59):\n", + " print \"Second Division\"\n", + "else:\n", + " print \"Fail\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks ? 84\n", + "Honours\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9 , Page number: CP-70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to calculate electirc charges for domestic consumers\n", + "# Variable declaration\n", + "\n", + "units = 348\n", + "\n", + "print \"Enter consumed units :\",units\n", + "\n", + "# Calculation of electric charges\n", + "\n", + "if (units <= 200):\n", + " amt = 0.5 * units\n", + "elif (units <= 400):\n", + " amt = 100 + 0.65 * (units - 200)\n", + "elif (units <= 600):\n", + " amt = 230 + 0.8 * (units - 400)\n", + "else:\n", + " amt = 425 + 1.25 * (units - 600)\n", + "print \n", + "print \"Amount to be paid Rs.%0.2f\" % amt" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter consumed units : 348\n", + "\n", + "Amount to be paid Rs.196.20\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10 , Page number: CP-73" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the grade of steel samples\n", + "# Variable declaration\n", + "\n", + "ts = 800\n", + "rh = 180\n", + "cc = 3\n", + "\n", + "print \"Enter tensile strength :\",ts\n", + "print \"Enter rockwell hardness :\",rh\n", + "print \"Enter carbon content :\",cc\n", + "\n", + "# Calculation of grade\n", + "\n", + "if (ts >= 700):\n", + " if (rh >= 200):\n", + " if (cc <= 6):\n", + " print \"Grade is A\"\n", + " else:\n", + " print \"Grade is B\" \n", + " elif (cc <= 6):\n", + " print \"Grade is C\"\n", + " else:\n", + " print \"Grade is E\" \n", + "elif (rh >= 200):\n", + " if (cc <= 6):\n", + " print \"Grade is D\"\n", + " else:\n", + " print \"Grade is E\"\n", + "elif (cc <= 6):\n", + " print \"Grade is E\"\n", + "else:\n", + " print \"Grade is F\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter tensile strength : 800\n", + "Enter rockwell hardness : 180\n", + "Enter carbon content : 3\n", + "Grade is C\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file 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 diff --git a/Computer_Programming_Theory_and_Practice/Chapter6.ipynb b/Computer_Programming_Theory_and_Practice/Chapter6.ipynb new file mode 100755 index 00000000..14969fc1 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter6.ipynb @@ -0,0 +1,794 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:aced1a8512bbd1c950b44a0ce98d7bb529f62e617c89b1c73eec08ce49b27311" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Arrays and Subscribed Variables" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-122" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the sum of n numbers using array\n", + "# Variable declaration\n", + "\n", + "n = 5\n", + "x = [36,45,52,44,62]\n", + "\n", + "print \"How many integers ?\",n\n", + "print \"Enter the 1th value :\",x[0]\n", + "print \"Enter the 2th value :\",x[1]\n", + "print \"Enter the 3th value :\",x[2]\n", + "print \"Enter the 4th value :\",x[3]\n", + "print \"Enter the 5th value :\",x[4]\n", + "\n", + "summ = 0 \n", + "\n", + "# Calculation of sum of numbers\n", + "for i in x:\n", + " summ = summ + i\n", + " \n", + "\n", + "print \"Sum of all integers =\",summ\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many integers ? 5\n", + "Enter the 1th value : 36\n", + "Enter the 2th value : 45\n", + "Enter the 3th value : 52\n", + "Enter the 4th value : 44\n", + "Enter the 5th value : 62\n", + "Sum of all integers = 239\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-123" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the biggest of n numbers\n", + "# Variable declaration\n", + "\n", + "n = 5\n", + "x = [25,-228,0,185,36]\n", + "\n", + "print \"How many numbers ?\",n\n", + "print \"Enter all those numbers\"\n", + "for a in x:\n", + " print a,\n", + "print \n", + "\n", + "big = x[0]\n", + "\n", + "\n", + "for i in x:\n", + " if (i > big):\n", + " big = i\n", + "\n", + "\n", + "print \"%d is the biggest number\" % big " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers ? 5\n", + "Enter all those numbers\n", + "25 -228 0 185 36\n", + "185 is the biggest number\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: Cp-124" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the arithmetic mean, variance and standard deviation\n", + "# Variable declaration\n", + "\n", + "n = 6\n", + "x = [3.1,3.8,3.6,4.0,3.4,3.8]\n", + "summ = 0\n", + "vsum = 0\n", + "\n", + "print \"How many values ?\",n\n", + "print \"Enter all values in the list \"\n", + "for a in x:\n", + " print a,\n", + "print \n", + "\n", + "# Loop to find sum of all values\n", + "\n", + "for i in x:\n", + " summ = summ + i\n", + "\n", + "xbar = summ / n\n", + "\n", + "# Loop to find the numerator vsum to find variance\n", + "\n", + "for i in x:\n", + " vsum = vsum + (i - xbar) * (i - xbar)\n", + "\n", + "\n", + "sigmax = vsum / n\n", + "sd = sigmax ** 0.5\n", + "\n", + "print \"Arithmetic mean = %0.3f\" % xbar\n", + "print \"Variance = %0.3f \" % sigmax\n", + "print \"Standard deviation = %0.3f\" % sd" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many values ? 6\n", + "Enter all values in the list \n", + "3.1 3.8 3.6 4.0 3.4 3.8\n", + "Arithmetic mean = 3.617\n", + "Variance = 0.088 \n", + "Standard deviation = 0.297\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to calculate mean of marks and print list of marks greater than mean\n", + "# Variable declaration\n", + "\n", + "n = 5\n", + "x = [58,63,68,54,48]\n", + "summ = 0 # Used summ instead of sum since it was a inbuilt function\n", + "i = 0\n", + "\n", + "print \"How many students ?\",n\n", + "print \"Enter all the marks \"\n", + "for a in x:\n", + " print a,\n", + "print \n", + "\n", + "for i in x:\n", + " summ = summ + i\n", + "\n", + "mean = float(summ) / n\n", + "\n", + "print \"Mean = %0.2f\" % mean\n", + "print \"Marks greater than mean :\",\n", + "\n", + "i = 0\n", + "for i in x:\n", + " if (i > mean):\n", + " print i,\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many students ? 5\n", + "Enter all the marks \n", + "58 63 68 54 48\n", + "Mean = 58.20\n", + "Marks greater than mean : 63 68\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-127" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find sum of all positive and negative numbers and to find out which is larger in magnitude\n", + "# Variable declaration\n", + "import math\n", + "\n", + "n = 6\n", + "x = [8,-12,-16,12,-9,5]\n", + "psum = 0\n", + "nsum = 0\n", + "\n", + "print \"How many values ?\",n\n", + "print \"Enter all values in the list\"\n", + "for i in x:\n", + " print i,\n", + "print \n", + "\n", + "# Loop to calculate sum of positive and negative values\n", + "\n", + "for i in x:\n", + " if i > 0:\n", + " psum = psum + i\n", + " else:\n", + " nsum = nsum + i\n", + "\n", + "print \"Sum of positive values = %0.2f\" % psum\n", + "print \"Sum of negative values = %0.2f\" % nsum\n", + "\n", + "if (psum > abs(nsum)):\n", + " print \"Positive sum is greater in magnitude\"\n", + "else:\n", + " print \"Negative sum is greater in magnitude\"\n", + "\n", + "diff = abs(psum) - abs(nsum)\n", + "print \"Difference in magnitude = %0.2f\" % abs(diff)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many values ? 6\n", + "Enter all values in the list\n", + "8 -12 -16 12 -9 5\n", + "Sum of positive values = 25.00\n", + "Sum of negative values = -37.00\n", + "Negative sum is greater in magnitude\n", + "Difference in magnitude = 12.00\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to sort n numbers in ascending order\n", + "# Variable declaration\n", + "\n", + "n = 4\n", + "x = [32,-10,20,5]\n", + "i = 0\n", + "\n", + "\n", + "print \"How many numbers ?\",n\n", + "print \"Enter the list of 4 numbers\"\n", + "for a in x:\n", + " print a,\n", + "print\n", + "\n", + "# Loop to arrange the numbers in ascending order\n", + "\n", + "while i < n-1:\n", + " j = i + 1\n", + " while j < n:\n", + " if x[i] > x[j]:\n", + " temp = x[i]\n", + " x[i] = x[j]\n", + " x[j] = temp\n", + " j = j + 1\n", + " i = i + 1\n", + "\n", + "print \"Numbers in ascending order \"\n", + "\n", + "for a in x:\n", + " print a,\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers ? 4\n", + "Enter the list of 4 numbers\n", + "32 -10 20 5\n", + "Numbers in ascending order \n", + "-10 5 20 32\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7 , Page number: CP-131" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to search the key value and to print it if the search is successful\n", + "# Variable declaration\n", + "\n", + "n = 6\n", + "x = [6,-2,8,3,13,10]\n", + "s = 3\n", + "\n", + "print \"How many values in the list ?\",n\n", + "print \"Enter all values in the list\"\n", + "for i in x:\n", + " print i,\n", + "print \n", + "print \"Enter the key value to be searched :\",s\n", + "\n", + "# loop to search key value in the list\n", + "\n", + "for i in range(n):\n", + " if s == x[i]:\n", + " print s,\" is available in\",i+1,\"th location\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many values in the list ? 6\n", + "Enter all values in the list\n", + "6 -2 8 3 13 10\n", + "Enter the key value to be searched : 3\n", + "3 is available in 4 th location\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8 , Page number: CP-133" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to sort n numbers using bubble sort and find number of exchanges and passes\n", + "# Variable declaration\n", + "\n", + "n = 4\n", + "x = [6,-2,8,3]\n", + "exchng = 0\n", + "\n", + "print \"How many numbers?\",n\n", + "print \"Enter all the numbers in the list\"\n", + "for i in x:\n", + " print i,\n", + "\n", + "print\n", + "\n", + "for i in range(0,n-1):\n", + " for j in range(0,n-i-1):\n", + " if x[j] > x[j+1]:\n", + " temp = x[j]\n", + " x[j] = x[j+1]\n", + " x[j+1] = temp\n", + " exchng = exchng + 1\n", + " \n", + "\n", + "print \"The sorted list is\"\n", + "for i in x:\n", + " print i,\n", + "\n", + "print \n", + "\n", + "print \"Sorted in\",n-1,\"passes and\",exchng,\"exchanges\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers? 4\n", + "Enter all the numbers in the list\n", + "6 -2 8 3\n", + "The sorted list is\n", + "-2 3 6 8\n", + "Sorted in 3 passes and 3 exchanges\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9 , Page number: CP-135" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to add two matrices\n", + "# Variable declaration\n", + "\n", + "a = [[2,-2],\n", + " [0,4]]\n", + "b = [[6,2],\n", + " [4,-5]]\n", + "\n", + "c = [[0,0],\n", + " [0,0]]\n", + "\n", + "m = 2\n", + "n = 2\n", + "\n", + "print \"How many rows and columns ?\",m,n\n", + "print \"Enter A matrix\"\n", + "for i in a:\n", + " print i\n", + "\n", + "print \"Enter B matrix\"\n", + "for j in b:\n", + " print j\n", + "\n", + "# Loop to add two matrices\n", + "\n", + "for i in range(m):\n", + " for j in range(n):\n", + " c[i][j] = a[i][j] + b[i][j]\n", + "\n", + "\n", + "print \"Resultant matrix is\"\n", + "for ci in c:\n", + " print ci" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows and columns ? 2 2\n", + "Enter A matrix\n", + "[2, -2]\n", + "[0, 4]\n", + "Enter B matrix\n", + "[6, 2]\n", + "[4, -5]\n", + "Resultant matrix is\n", + "[8, 0]\n", + "[4, -1]\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10 , Page number: CP-136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to multiply two matrices\n", + "# Variable declaration\n", + "\n", + "m = 2\n", + "n = 2\n", + "l = 2\n", + "a = [[2,-2],\n", + " [0,4]]\n", + "b = [[6,2],\n", + " [4,-5]]\n", + "c = [[0,0],\n", + " [0,0]]\n", + "\n", + "print \"Enter order of A matrix :\",m,n\n", + "print \"Enter A matrix\"\n", + "for i in a:\n", + " print i\n", + "print \"Enter order of B matrix :\",m,n\n", + "print \"Enter B matrix\"\n", + "for j in b:\n", + " print j\n", + "\n", + "# Loop to multiply two matrices\n", + "# iterate through rowa of A\n", + "for i in range(m):\n", + " # iterate through columns of B\n", + " for j in range(l):\n", + " c[i][j] = 0\n", + " # iterate through rows of B\n", + " for k in range(n):\n", + " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", + "\n", + "\n", + "print \"Resultant matrix is\"\n", + "\n", + "for i in c:\n", + " print i\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter order of A matrix : 2 2\n", + "Enter A matrix\n", + "[2, -2]\n", + "[0, 4]\n", + "Enter order of B matrix : 2 2\n", + "Enter B matrix\n", + "[6, 2]\n", + "[4, -5]\n", + "Resultant matrix is\n", + "[4, 14]\n", + "[16, -20]\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11 , Page number: CP-138" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find and print the transpose of the matrix\n", + "# Variable declaration\n", + "\n", + "m = 2\n", + "n = 3\n", + "a = [[-3,6,0],\n", + " [3,2,8]]\n", + "at = [[0,0],\n", + " [0,0],\n", + " [0,0]]\n", + "\n", + "print \"Enter order of the matrix :\",m,n\n", + "print \"Enter the matrix values\"\n", + "for i in a :\n", + " print i\n", + "\n", + "# Loop to calculate transpose\n", + "\n", + "for i in range(m):\n", + " for j in range(n):\n", + " at[j][i] = a[i][j]\n", + "\n", + "print \"The transposed matrix is \"\n", + "for i in at:\n", + " print i\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter order of the matrix : 2 3\n", + "Enter the matrix values\n", + "[-3, 6, 0]\n", + "[3, 2, 8]\n", + "The transposed matrix is \n", + "[-3, 3]\n", + "[6, 2]\n", + "[0, 8]\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12 ,Page number: CP-139" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to check whether a given matrix is symmetric or not\n", + "# Variable declaration\n", + "\n", + "m = 3\n", + "a = [[5,3,8],\n", + " [3,1,-7],\n", + " [8,-7,4]]\n", + "\n", + "print \"Enter order of the square matrix :\",m\n", + "for i in a:\n", + " print i\n", + "\n", + "# Loop to check whether symmetric or not\n", + "\n", + "for i in range(m):\n", + " flag = 0\n", + " for j in range(m):\n", + " flag = 0\n", + " if a[i][j] == a[j][i]:\n", + " continue\n", + " else:\n", + " flag = 1\n", + "\n", + "if flag == 0:\n", + " print \"The given matrix is a symmetric matrix\"\n", + "else:\n", + " print \"The given matrix is not a symmetric matrix\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter order of the square matrix : 3\n", + "[5, 3, 8]\n", + "[3, 1, -7]\n", + "[8, -7, 4]\n", + "The given matrix is a symmetric matrix\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13 , Page number: CP-141" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the trace of a given square matrix\n", + "# Variable dclaration\n", + "\n", + "m = 3\n", + "a = [[3,2,-1],\n", + " [4,1,8],\n", + " [6,4,2]]\n", + "summ = 0\n", + "\n", + "print \"Enter order of the square matrix :\",m\n", + "print \"Enter the matrix\"\n", + "for i in a:\n", + " print i\n", + "\n", + "# Loop to find trace\n", + "for i in range(m):\n", + " summ = summ + a[i][i]\n", + "\n", + "print \"Trace of the matrix =\",summ " + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter7.ipynb b/Computer_Programming_Theory_and_Practice/Chapter7.ipynb new file mode 100755 index 00000000..0d79c17f --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter7.ipynb @@ -0,0 +1,557 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:37ae6cc76fe6f4a8cc8dba62582a6a103f99da5c7579d1a782cb95241f542fd8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to count the occurence of a particular in a string\n", + "# Variable declaration\n", + "\n", + "st = \"MISSISSIPPI\"\n", + "ch = \"S\"\n", + "count = 0\n", + "\n", + "print \"Enter the string :\",st\n", + "print \"Which character to be counted ?\",ch\n", + "\n", + "# Loop to check occurrence of aa character\n", + "\n", + "l = len(st)\n", + "\n", + "for i in st:\n", + " if i == ch:\n", + " count = count + 1\n", + "\n", + "print \"The character \" + ch + \" occurs %d times\" % count " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the string : MISSISSIPPI\n", + "Which character to be counted ? S\n", + "The character S occurs 4 times\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to count the number of vowels in a sentence\n", + "# Variable declaration\n", + "\n", + "st = \"This is a book\"\n", + "count = 0\n", + "\n", + "print \"Enter the sentence :\"\n", + "print st\n", + "\n", + "# Loop to count the vowels in the string\n", + "\n", + "for i in st:\n", + " if i == \"A\":\n", + " count = count + 1\n", + " elif i == \"E\":\n", + " count = count + 1\n", + " elif i == \"I\":\n", + " count = count + 1\n", + " elif i == \"O\":\n", + " count = count + 1\n", + " elif i == \"U\":\n", + " count = count + 1\n", + " elif i == \"a\":\n", + " count = count + 1\n", + " elif i == \"e\":\n", + " count = count + 1\n", + " elif i == \"i\":\n", + " count = count + 1\n", + " elif i == \"o\":\n", + " count = count + 1\n", + " elif i == \"u\":\n", + " count = count + 1\n", + "\n", + "print \"%d vowels are present in the sentence\" % count\n", + " \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the sentence :\n", + "This is a book\n", + "5 vowels are present in the sentence\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: CP-150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to test whether a given string is a palindrome or not\n", + "# Variable declaration\n", + "\n", + "st = \"HYDERABAD\"\n", + "rst = \"\"\n", + "i = 0\n", + "j = len(st) - 1\n", + "\n", + "print \"Enter the string :\",st\n", + "\n", + "# Palindrome or not \n", + "\n", + "rst = reversed(st)\n", + "\n", + "if list(st) == list(rst):\n", + " print \"%s is a palindrome string \" % st\n", + "else:\n", + " print \"%s is not a palindrome string \" % st" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the string : HYDERABAD\n", + "HYDERABAD is not a palindrome string \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to conctenate two strings\n", + "# Variable declaration\n", + "\n", + "st1 = \"NEW \"\n", + "st2 = \"DELHI\"\n", + "\n", + "# input of two strings to be concatenated\n", + "\n", + "print \"Enter first string :\",st1\n", + "print \"Enter second string :\",st2\n", + "\n", + "# concatenation of two strings\n", + "st = st1 + st2\n", + "\n", + "print \"Resultant string is \",st " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first string : NEW \n", + "Enter second string : DELHI\n", + "Resultant string is NEW DELHI\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to compare two strings\n", + "# Variable declaration\n", + "\n", + "st1 = \"ALPHA\"\n", + "st2 = \"BETA\"\n", + "\n", + "print \"Enter string 1:\",st1\n", + "print \"Enter string 2:\",st2\n", + "\n", + "# compare strings\n", + "\n", + "if (cmp(st1,st2)>0):\n", + " print \"%s \" + st1 + \"is alphabetically greater string\"\n", + "else:\n", + " print st2 + \" is alphabetically greater string\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter string 1: ALPHA\n", + "Enter string 2: BETA\n", + "BETA is alphabetically greater string\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-155" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to sort an array of names in alphabetical order\n", + "# Variable declaration\n", + "\n", + "n = 4\n", + "names = [\"DEEPAK\",\"SHERIN\",\"SONIKA\",\"ARUN\"]\n", + "\n", + "print \"How many names ?\",n\n", + "print \"Enter the 4 names one by one\"\n", + "for i in names:\n", + " print i\n", + "\n", + "# Loop to arrange names in alphabetical order\n", + "\n", + "for i in range(0,n-1):\n", + " for j in range(i+1,n):\n", + " if cmp(names[i],names[j])>0:\n", + " \n", + " temp = names[i]\n", + " names[i] = names[j]\n", + " names[j] = temp\n", + "\n", + "print \"Names in alphabetical order\"\n", + "for i in names:\n", + " print i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many names ? 4\n", + "Enter the 4 names one by one\n", + "DEEPAK\n", + "SHERIN\n", + "SONIKA\n", + "ARUN\n", + "Names in alphabetical order\n", + "ARUN\n", + "DEEPAK\n", + "SHERIN\n", + "SONIKA\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7 , Page number: CP-157" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to convert a line from lower case to upper case\n", + "# Variable declaretion\n", + "\n", + "import sys\n", + "\n", + "st = ['l','o','g','i','c','a','l',' ','t','h','i','n','k','i','n','g',' ','i','s',' ','a',' ','m','u','s','t',' ','t','o',' ','l','e','a','r','n',' ','p','r','o','g','r','a','m','m','i','n','g']\n", + "\n", + "print \"Enter a sentence :\"\n", + "for i in st:\n", + " print i,\n", + "print \n", + "print \"The converted upper case string is\"\n", + "# loop to convert lower case alphabet to upper case text\n", + "for i in range(len(st)):\n", + " if st[i] >= 'a' and st[i] <= 'z':\n", + " st[i] = chr(ord(st[i])-32)\n", + "\n", + "\n", + " sys.stdout.write(st[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a sentence :\n", + "l o g i c a l t h i n k i n g i s a m u s t t o l e a r n p r o g r a m m i n g\n", + "The converted upper case string is\n", + "LOGICAL THINKING IS A MUST TO LEARN PROGRAMMING" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9 , Page number: CP-160" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to read a text and to omit al occurrences of a particular word\n", + "# Variable declartion\n", + "\n", + "st = \"TO ACCESS THE NAME OF THE CITY IN THE LIST\"\n", + "i = 0\n", + "omit = \"THE\"\n", + "l = len(st)\n", + "j = 0\n", + "word = []\n", + "newst = \"\"\n", + "onesps = \"\"\n", + "print \"Enter a sentence :\"\n", + "print st\n", + "print \"Enter word to omit :\",omit\n", + "\n", + "# loop to omit the given word\n", + "\n", + "for i in range(l):\n", + " ch = i\n", + " if ch == ' ':\n", + " for j in word:\n", + " j = \" \" \n", + " if j == omit:\n", + " newst = j\n", + " newst = onesps\n", + " j = \" \"\n", + " j = 0\n", + " else:\n", + " j = ch\n", + " j = j + 1\n", + " i = i + 1\n", + "\n", + "print \"After omiting the word \" + omit\n", + "print newst\n", + "print \"Press any key to continue\"\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a sentence :\n", + "TO ACCESS THE NAME OF THE CITY IN THE LIST\n", + "Enter word to omit : THE\n", + "After omiting the word THE\n", + "\n", + "Press any key to continue\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10 , Page number: CP-161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to calculate the amount to be paid for the telegram\n", + "# Variable declaration\n", + "\n", + "count = 0\n", + "st = \"Congratulations on your success in Examinations.\"\n", + "l = len(st)\n", + "\n", + "print \"Type the sentence for Telegram\"\n", + "print st\n", + "\n", + "# loop to count number of words\n", + "\n", + "for i in range(l):\n", + " if st[i] == '?':\n", + " count = count + 1\n", + "\n", + "if count <= 10:\n", + " amt = 5\n", + "else:\n", + " amt = 5 + (count - 10) * 1.25\n", + "\n", + "print \"Amount to be paid for telegram = Rs.%0.2f\" % amt " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Type the sentence for Telegram\n", + "Congratulations on your success in Examinations.\n", + "Amount to be paid for telegram = Rs.5.00\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11 , Page number: CP-163" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to count number of lines,words and characters\n", + "# Variable declaration\n", + "\n", + "txt = \"What is a string? How do you initialize it? Explain with example.$\"\n", + "st = \"\"\n", + "i = 0\n", + "lns = 0\n", + "wds = 0\n", + "chs = 0\n", + "\n", + "print \"Enter the text, type $ at end.\"\n", + "print txt\n", + "\n", + "# loop to count lines,words and characters in text\n", + "\n", + "while txt[i] != '$':\n", + " # switch case statements\n", + " if txt[i] == ' ':\n", + " wds = wds + 1\n", + " chs = chs + 1\n", + " elif txt[i] == '.':\n", + " wds = wds + 1\n", + " lns = lns + 1\n", + " chs = chs + 1\n", + " elif txt[i] == '?':\n", + " lns = lns + 1\n", + " else: # default\n", + " chs = chs + 1\n", + "\n", + " i = i + 1\n", + "\n", + "print \"Number of char (incl. blanks) =\",chs\n", + "print \"Number of words =\",wds\n", + "print \"Number of lines =\",lns\n", + "\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the text, type $ at end.\n", + "What is a string? How do you initialize it? Explain with example.$\n", + "Number of char (incl. blanks) = 63\n", + "Number of words = 12\n", + "Number of lines = 3\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter8.ipynb b/Computer_Programming_Theory_and_Practice/Chapter8.ipynb new file mode 100755 index 00000000..49edb490 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter8.ipynb @@ -0,0 +1,1027 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:0c7b2e51d9ff140f666ba709dd09beff8baf4fe046109218b6f3ac69ed4bb2fe" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to write a function to find factorial and use it to find nCr\n", + "# Variable declaration\n", + "\n", + "n = 5\n", + "r = 3\n", + "\n", + "print \"Enter value to n and r :\",n,r\n", + "\n", + "# function subprogram to find factorial\n", + "\n", + "def fact(k):\n", + " p = 1\n", + " for i in range(1,k+1):\n", + " p = p * i\n", + " return p\n", + "\n", + "\n", + "ncr = fact(n) / (fact(r) * fact(n-r))\n", + "\n", + "\n", + "print \"Value of nCr =\",ncr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to n and r : 5 3\n", + "Value of nCr = 10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-174" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the biggest of given three values using function and use it to find total marks of the student\n", + "# Variable declaration\n", + "\n", + "t1 = 62\n", + "t2 = 70\n", + "t3 = 58\n", + "a1 = 17\n", + "a2 = 21\n", + "a3 = 23\n", + "\n", + "print \"Enter three test scores :\",t1,t2,t3\n", + "print \"Enter three assignment scores :\",a1,a2,a3\n", + "\n", + "# function to find biggest of three values\n", + "\n", + "def big(a,b,c):\n", + " if a > b:\n", + " if a > c:\n", + " return a\n", + " else:\n", + " return c\n", + " elif b > c:\n", + " return b\n", + " else:\n", + " return c\n", + "\n", + "total = big(t1,t2,t3) + big(a1,a2,a3)\n", + "\n", + "print \"Total marks =\",total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three test scores : 62 70 58\n", + "Enter three assignment scores : 17 21 23\n", + "Total marks = 93\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: CP-176" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to create a function to compute the cos(x) series upto 15 terms\n", + "# Variable declaration\n", + "\n", + "x = 0\n", + "\n", + "print \"----------------------------------\"\n", + "print \" x in degrees cos(x) \"\n", + "print \"----------------------------------\"\n", + "\n", + "# function for computing cosine function\n", + "\n", + "def cosine(x):\n", + " s = 0\n", + " i = 0\n", + " term = 1\n", + " x = x * (3.14) / 180\n", + " k = 1\n", + " # loop to find summation of 15 terms\n", + " while k <= 15:\n", + " s = s + term\n", + " term = term * x * x * (-1) / ((i + 1) * (i + 2))\n", + " i = i + 2\n", + " k = k + 1\n", + " return s\n", + "\n", + "\n", + "# calling function\n", + "\n", + "while x <= 180:\n", + " print \" %d \" % x,\" %0.2f \"%cosine(x)\n", + " x = x + 30\n", + "\n", + "print \"-----------------------------------\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "----------------------------------\n", + " x in degrees cos(x) \n", + "----------------------------------\n", + " 0 1.00 \n", + " 30 0.87 \n", + " 60 0.50 \n", + " 90 0.00 \n", + " 120 -0.50 \n", + " 150 -0.87 \n", + " 180 -1.00 \n", + "-----------------------------------\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-177" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find GCD of two integers\n", + "# Variable declaration\n", + "\n", + "a = 45\n", + "b = 27\n", + "c = 81\n", + "\n", + "print \"Enter three integers :\",a,b,c\n", + "\n", + "def gcd(x,y):\n", + " if x >= y:\n", + " nr = x\n", + " dr = y\n", + " else:\n", + " nr = y\n", + " dr = x\n", + " r = nr % dr\n", + " while r != 0:\n", + " nr = dr\n", + " dr = r\n", + " r = nr % dr\n", + "\n", + " return dr\n", + "\n", + "d1 = gcd(a,b)\n", + "d2 = gcd(a,c)\n", + "d3 = gcd(b,c)\n", + "\n", + "if d1 == d2:\n", + " if d1 == d3:\n", + " print \"Greatest common divisor is\",d1\n", + " else:\n", + " print \"Greatest common divisor is \",gcd(d1,d3)\n", + "else:\n", + " print \"Greatest common divisr is\",gcd(d1,d2)\n", + "\n", + "\n", + "print \"Press any key to continue. . .\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter three integers : 45 27 81\n", + "Greatest common divisor is 9\n", + "Press any key to continue. . .\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-178" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to reverse the given integer\n", + "# Variable declaration\n", + "\n", + "n = 2846\n", + "\n", + "print \"Enter the integer :\",n\n", + "\n", + "# function to reverse an integer\n", + "\n", + "def reverse(n):\n", + " rn = 0\n", + " while n > 0:\n", + " r = n % 10\n", + " rn = rn * 10 + r\n", + " n = n /10\n", + " return rn\n", + "\n", + "\n", + "# call the function to print the reverse integer\n", + "\n", + "print n,\"is reversed as \",reverse(n)\n", + "print \"Press any key to continue. . .\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the integer : 2846\n", + "2846 is reversed as 6482\n", + "Press any key to continue. . .\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-179" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to compare two strings S1 and S2 and return the result 0,1,-1\n", + "# Variable declaration\n", + "\n", + "s1 = \"MUMBAI\"\n", + "s2 = \"MYSORE\"\n", + "\n", + "print \"Enter the first string :\",s1\n", + "print \"Enter the second string :\",s2\n", + "\n", + "\n", + "# function to compare the strings\n", + "\n", + "def compare(s1,s2):\n", + " if cmp(s1,s2) == 0:\n", + " return 0\n", + " if cmp(s1,s2) > 0:\n", + " return 1\n", + " if cmp(s1,s2) < 0:\n", + " return -1\n", + "\n", + "# call the function to print the result\n", + "\n", + "print \"The result is \", compare(s1,s2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the first string : MUMBAI\n", + "Enter the second string : MYSORE\n", + "The result is -1\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7 , Page number: CP-181" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find the arithmetic mean of n values using a function\n", + "# Variable declaration\n", + "\n", + "n = 6\n", + "x = [3.1,3.8,3.6,4.0,3.4,3.8]\n", + "\n", + "print \"How many values ?\",n\n", + "print \"Enter all values\"\n", + "for i in x:\n", + " print i,\n", + "print \n", + "# function to find arithmetic mean\n", + "\n", + "def amean(x,n):\n", + " s = 0\n", + " for i in range(n):\n", + " s = s + x[i]\n", + " return (s/n)\n", + "\n", + "# call function to print arithmetic mean\n", + "\n", + "print \"Arithmetic mean = %0.2f\" % amean(x,n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many values ? 6\n", + "Enter all values\n", + "3.1 3.8 3.6 4.0 3.4 3.8\n", + "Arithmetic mean = 3.62\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8 , Page number: CP-182" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to read a matrix of order m x n and print the sum of all elements using functions\n", + "# Variable declaration\n", + "\n", + "m = 3\n", + "n = 2\n", + "a = [[2,3,4],\n", + " [2,0,1]]\n", + "\n", + "\n", + "print \"How many rows and columns :\",m,n\n", + "print \"Enter the matrix values\"\n", + "for i in range(n):\n", + " for j in range(m):\n", + " print a[i][j],\n", + " print \n", + "\n", + "# function to add all elements in the matrix\n", + "def elem_sum(a,m,n):\n", + " i = 0\n", + " j = 0\n", + " s = 0\n", + " for i in range(n):\n", + " for j in range(m):\n", + " s = s + a[i][j]\n", + " j = j + 1\n", + " i = i + 1 \n", + " return s\n", + "\n", + "# call function to print the result\n", + "\n", + "print \"Sum of all elements in the matrix =\",\n", + "print elem_sum(a,m,n)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows and columns : 3 2\n", + "Enter the matrix values\n", + "2 3 4\n", + "2 0 1\n", + "Sum of all elements in the matrix = 12\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9 , Page number: CP-183" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to reverse a string\n", + "# Variable declaration\n", + "\n", + "st = \"NEW DELHI\"\n", + "print \"Enter a string :\",st\n", + "def reverse(st):\n", + " rst = \"\"\n", + " for i in range(0 ,len(st)):\n", + " rst += st[(len(st) -1) - i]\n", + " return rst\n", + "\n", + "print st,\"is reversed as \",\n", + "print reverse(st)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string : NEW DELHI\n", + "NEW DELHI is reversed as IHLED WEN\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10 , Page number: CP-184" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to apply binary search to set of N numbers using a function\n", + "# Variable declaration\n", + "\n", + "n = 10\n", + "x = [-3,8,13,19,21,25,26,29,35,42]\n", + "s = 19\n", + "\n", + "\n", + "print \"How many numbers ?\",n\n", + "print \"Enter all numbers in the list\"\n", + "for a in x:\n", + " print a,\n", + "print \n", + "print \"Enter the number to be searched :\",s\n", + "\n", + "# function to search the number\n", + "\n", + "def bi_search(x,n,s):\n", + " flag = 0\n", + " start = 0\n", + " end = n\n", + " while start < end and flag ==0:\n", + " mid = (start + end) / 2\n", + " if x[mid] > s:\n", + " end = mid\n", + " elif x[mid] < s:\n", + " start = mid + 1\n", + " else:\n", + " flag = 1\n", + " return flag\n", + "\n", + "# calling the function\n", + "\n", + "if bi_search(x,n,s):\n", + " print \"The number\",s,\"is present in the list\"\n", + "else:\n", + " print \"The number\",s,\"is not present in list\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers ? 10\n", + "Enter all numbers in the list\n", + "-3 8 13 19 21 25 26 29 35 42\n", + "Enter the number to be searched : 19\n", + "The number 19 is present in the list\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11 , Page number: CP-187" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to find factorial of a given number using recursive function\n", + "# Variable declaration\n", + "\n", + "n = 5\n", + "r = 3\n", + "\n", + "print \"Enter the values to n and r :\",n,r\n", + "\n", + "# recursive function to find factorial\n", + "\n", + "def fact(k):\n", + " if k ==1:\n", + " return 1\n", + " else:\n", + " return (k * fact(k-1))\n", + "\n", + "ncr = fact(n)/(fact(r) * fact(n-r))\n", + "print \"Value of nCr =\",ncr " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the values to n and r : 5 3\n", + "Value of nCr = 10\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12 , Page number: CP-188" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to compute the value of x power n using a recursive function\n", + "# Variable declaration\n", + "\n", + "x = 4.20\n", + "n = 3\n", + "\n", + "print \"Enter value to x :\",x\n", + "print \"Enter its power :\",n\n", + "\n", + "# recursive function to find x rise to n\n", + "\n", + "def power(x,n):\n", + " if n == 1:\n", + " return x\n", + " else:\n", + " return (x * power(x,n-1))\n", + "\n", + "\n", + "print \"%0.2f\" %x,\"raise to \",n,\"is %0.2f\" % power(x,n) " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value to x : 4.2\n", + "Enter its power : 3\n", + "4.20 raise to 3 is 74.09\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13 , Page number: CP-189" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to display first n terms of the fibonacci series using recursive function\n", + "\n", + "# Variable declaration\n", + "\n", + "n = 10\n", + "t1 = 0\n", + "t2 = 1\n", + "i = 0\n", + "count = 2\n", + "\n", + "# recursive function to print the terms in series\n", + "def fibo(t1,t2):\n", + " global count\n", + " if (count >= n):\n", + " return\n", + " else:\n", + " t3 = t1 + t2\n", + " print t3,\n", + " count = count + 1\n", + " t1 = t2\n", + " t2 = t3\n", + " return fibo(t1,t2)\n", + " \n", + "\n", + "\n", + "print \"How many terms to be printed ?\",n\n", + "print \"The first\",n,\"terms in Fibonacci series are\"\n", + "print t1,\n", + "print t2,\n", + "\n", + "fibo(t1,t2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many terms to be printed ? 10\n", + "The first 10 terms in Fibonacci series are\n", + "0 1 1 2 3 5 8 13 21 34\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 14 , Page number: CP-196" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to add two matrices of order m x n and to print the resultant values\n", + "# Variable declaration\n", + "\n", + "m = 2\n", + "n = 2\n", + "a = [[2,-2],\n", + " [0,4]]\n", + "b = [[6,2],\n", + " [4,-5]]\n", + "c = [[0,0],\n", + " [0,0]]\n", + "\n", + "print \"How many rows and columns ?\",m,n\n", + "print \"Enter A matrix value\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print a[i][j],\n", + " print \n", + "print \"Enter B matrix values\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print b[i][j],\n", + " print \n", + "\n", + "# function to add matrices\n", + "def matadd():\n", + " for i in range(m):\n", + " for j in range(n):\n", + " c[i][j] = a[i][j] + b[i][j]\n", + "\n", + "\n", + "\n", + "# call function\n", + "matadd()\n", + "print \"Resultant matrix is\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print c[i][j],\n", + " print \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows and columns ? 2 2\n", + "Enter A matrix value\n", + "2 -2\n", + "0 4\n", + "Enter B matrix values\n", + "6 2\n", + "4 -5\n", + "Resultant matrix is\n", + "8 0\n", + "4 -1\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 15 , Page number: CP-197" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to multiply A matrix of order of m x n with B matrix of order n x l and print the resultant matrix\n", + "# Variable declaration\n", + "\n", + "a = [[2,-2],\n", + " [0,4]]\n", + "b = [[6,2,],\n", + " [4,-5]]\n", + "c = [[0,0],\n", + " [0,0]]\n", + "\n", + "m = 2\n", + "n = 2\n", + "l = 2\n", + "\n", + "print \"Enter order of A matrix (m x n) :\",m,n\n", + "print \"Enter A matrix values\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print a[i][j],\n", + " print\n", + " \n", + "print \"Enter order of B matrix (n x l) :\",n,l\n", + "print \"Enter B matrix values\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print b[i][j],\n", + " print \n", + " \n", + "\n", + "# function to multiply matrices\n", + "\n", + "def matmul():\n", + " for i in range(m):\n", + " for j in range(l):\n", + " c[i][j] = 0\n", + " for k in range(n):\n", + " c[i][j] = c[i][j] + a[i][k] * b[k][j]\n", + " return c\n", + "\n", + "# call function\n", + "matmul()\n", + "print \"Resultant matix is\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print c[i][j],\n", + " print\n", + "\n", + "\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter order of A matrix (m x n) : 2 2\n", + "Enter A matrix values\n", + "2 -2\n", + "0 4\n", + "Enter order of B matrix (n x l) : 2 2\n", + "Enter B matrix values\n", + "6 2\n", + "4 -5\n", + "Resultant matix is\n", + "4 14\n", + "16 -20\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16 , Page number: CP-199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to transpose a matrix of order m x n\n", + "# Variable declaration\n", + "\n", + "a = [[-3,6,0],\n", + " [3,2,8]]\n", + "at = [[0,0],\n", + " [0,0],\n", + " [0,0]]\n", + "m = 2\n", + "n = 3\n", + "ch = \"y\"\n", + "\n", + "print \"How many rows and columns ?\",m,n\n", + "print \"Enter the matrix values\"\n", + "for i in range(m):\n", + " for j in range(n):\n", + " print a[i][j],\n", + " print \n", + " \n", + "\n", + "# function to transpose a matrix\n", + "\n", + "def transpose(a,at,m,n):\n", + " for i in range(m):\n", + " for j in range(n):\n", + " at[j][i] = a[i][j]\n", + " return at\n", + "\n", + "while ch == 'y' or ch == 'Y' :\n", + " # call function to transpose the matrix\n", + " transpose(a,at,m,n)\n", + " print \"Transpose of the matrix is\"\n", + " for i in range(n):\n", + " for j in range(m):\n", + " print at[i][j],\n", + " print\n", + " ch = \"N\"\n", + " print \"Press y to continue\"\n", + " print \" any other key to stop.\",ch" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows and columns ? 2 3\n", + "Enter the matrix values\n", + "-3 6 0\n", + "3 2 8\n", + "Transpose of the matrix is\n", + "-3 3\n", + "6 2\n", + "0 8\n", + "Press y to continue\n", + " any other key to stop. N\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 17 , Page number: CP-200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to create a function to sort the elements of an array in ascending order\n", + "# Variable declaration\n", + "\n", + "n = 4\n", + "x = [32,-10,20,5]\n", + "\n", + "print \"How many numbers ?\",n\n", + "print \"Enter the list of values\"\n", + "for a in x:\n", + " print a,\n", + "print \n", + "\n", + "# function to sort the numbers\n", + "def sort(x,n):\n", + " for i in range(n-1):\n", + " for j in range(i+1,n):\n", + " if x[i] > x[j]:\n", + " temp = x[i]\n", + " x[i] = x[j]\n", + " x[j] = temp\n", + " return x\n", + "\n", + "# call function to sort the numbers\n", + "sort(x,n)\n", + "print \"The sorted list is\"\n", + "for a in x:\n", + " print a,\n", + "print\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many numbers ? 4\n", + "Enter the list of values\n", + "32 -10 20 5\n", + "The sorted list is\n", + "-10 5 20 32\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/Chapter9.ipynb b/Computer_Programming_Theory_and_Practice/Chapter9.ipynb new file mode 100755 index 00000000..2113c5af --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/Chapter9.ipynb @@ -0,0 +1,624 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:93c2825491a65ebd6dbc85e6208fc8a03cf21f92da06cf2457eda56a011645f1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 , Page number: CP-211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to display student details\n", + "# class declaration instead of a structure\n", + "\n", + "class student:\n", + " rno = 0\n", + " sname = \"\"\n", + " tot = 0\n", + "\n", + "# class object variable\n", + "\n", + "x = student()\n", + "x.rno = 20147\n", + "x.sname = \"PRADEEP\"\n", + "x.tot = 64\n", + "\n", + "print \"Enter roll number, name and total marks\"\n", + "print x.rno,x.sname,x.tot\n", + "print \" Details entered are\"\n", + "print \"Roll No. :\",x.rno\n", + "print \"Student name :\",x.sname\n", + "print \"Total marks :\",x.tot\n", + "print \"Press any key to continue. . .\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter roll number, name and total marks\n", + "20147 PRADEEP 64\n", + " Details entered are\n", + "Roll No. : 20147\n", + "Student name : PRADEEP\n", + "Total marks : 64\n", + "Press any key to continue. . .\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 , Page number: CP-212" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to input student details and print the marks of a specified student as output\n", + "\n", + "# class declaration for structure\n", + "\n", + "class student:\n", + " def __init__(self,rno,sname,tot):\n", + " self.rno = rno\n", + " self.sname = sname\n", + " self.tot = tot\n", + " \n", + "# variable declaration\n", + "\n", + "ch = \"y\"\n", + "n = 3\n", + "\n", + "# details of n students\n", + "std = []\n", + "std.append(student(20201,\"ARUN\",78))\n", + "std.append(student(20208,\"DEEPAK\",69))\n", + "std.append(student(20223,\"SUSMITHA\",88))\n", + "\n", + "print \"How many students ?\",n\n", + "print \"Roll number ?\",std[0].rno\n", + "print \"Name ?\",std[0].sname\n", + "print \"Total marks ?\",std[0].tot\n", + "print \"Roll number ?\",std[1].rno\n", + "print \"Name ?\",std[1].sname\n", + "print \"Total marks ?\",std[1].tot\n", + "print \"Roll number ?\",std[2].rno\n", + "print \"Name ?\",std[2].sname\n", + "print \"Total marks ?\",std[2].tot\n", + "print \n", + "\n", + "\n", + "# To display marks of the student\n", + "while ch == \"y\" or ch == \"Y\":\n", + " temp = 20208\n", + " print \"Enter student roll number to display marks :\",temp\n", + " print \n", + " flag = 0\n", + " #loop to search and display details\n", + " for i in range(3):\n", + " if flag == 0:\n", + " if std[i].rno == temp:\n", + " print \"Marks obtained by \",std[i].rno,std[i].sname\n", + " print \"Total :\",std[i].tot\n", + " flag = 1\n", + " if flag == 0:\n", + " print temp,\" is not present in the list \"\n", + " \n", + " ch = \"n\" \n", + " print \"press y - to continue\"\n", + " print \" any other key to stop.\",ch\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many students ? 3\n", + "Roll number ? 20201\n", + "Name ? ARUN\n", + "Total marks ? 78\n", + "Roll number ? 20208\n", + "Name ? DEEPAK\n", + "Total marks ? 69\n", + "Roll number ? 20223\n", + "Name ? SUSMITHA\n", + "Total marks ? 88\n", + "\n", + "Enter student roll number to display marks : 20208\n", + "\n", + "Marks obtained by 20208 DEEPAK\n", + "Total : 69\n", + "press y - to continue\n", + " any other key to stop. n\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 , Page number: CP-214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to declare a structure for student details and display list of students who obtained more than 75 marks\n", + "\n", + "# class for student structure\n", + "\n", + "class student:\n", + " def __init__(self,rno,sname,tot):\n", + " self.rno = rno\n", + " self.sname = sname\n", + " self.tot = tot\n", + "\n", + "std = []\n", + "std.append(student(30401,\"ANAND\",59))\n", + "std.append(student(30404,\"NIRMAL\",64))\n", + "std.append(student(30428,\"ISWARYA\",82))\n", + "std.append(student(30432,\"VIVEKA\",79))\n", + "\n", + "n = 4\n", + "print \"How many students ?\",n\n", + "print \"Roll Number ?\",std[0].rno\n", + "print \"Name ?\",std[0].sname\n", + "print \"Total marks ?\",std[0].tot\n", + "print \"Roll Number ?\",std[1].rno\n", + "print \"Name ?\",std[1].sname\n", + "print \"Total marks ?\",std[1].tot\n", + "print \"Roll Number ?\",std[2].rno\n", + "print \"Name ?\",std[2].sname\n", + "print \"Total marks ?\",std[2].tot\n", + "print \"Roll Number ?\",std[3].rno\n", + "print \"Name ?\",std[3].sname\n", + "print \"Total marks ?\",std[3].tot\n", + "print \n", + "\n", + "\n", + "print \"----------------------------------------------------\"\n", + "print \" Roll No. Name Total marks \"\n", + "print \"----------------------------------------------------\"\n", + "for i in range(n):\n", + " if std[i].tot >= 75:\n", + " print \" \",std[i].rno,\" \",std[i].sname,\" \",std[i].tot\n", + "\n", + "print \"----------------------------------------------------\"\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many students ? 4\n", + "Roll Number ? 30401\n", + "Name ? ANAND\n", + "Total marks ? 59\n", + "Roll Number ? 30404\n", + "Name ? NIRMAL\n", + "Total marks ? 64\n", + "Roll Number ? 30428\n", + "Name ? ISWARYA\n", + "Total marks ? 82\n", + "Roll Number ? 30432\n", + "Name ? VIVEKA\n", + "Total marks ? 79\n", + "\n", + "----------------------------------------------------\n", + " Roll No. Name Total marks \n", + "----------------------------------------------------\n", + " 30428 ISWARYA 82\n", + " 30432 VIVEKA 79\n", + "----------------------------------------------------\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 , Page number: CP-216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to store employee information and to compute employee's pay\n", + "\n", + "import math\n", + "# class declaration for employee\n", + "class employee:\n", + " def __init__(self,eno,ename,epay,jdate):\n", + " self.eno = eno\n", + " self.ename = ename\n", + " self.epay = epay\n", + " self.jdate = jdate\n", + "\n", + "employs = []\n", + "employs.append(employee(20101,\"ASHIKA\",1000,\"31/04/2001\"))\n", + "employs.append(employee(20182,\"ASHWIN\",6000,\"11/12/1995\"))\n", + "employs.append(employee(20204,\"PRAVEEN\",3000,\"18/06/1994\"))\n", + "\n", + "n = 3\n", + "\n", + "print \"Employee No. ?\",employs[0].eno\n", + "print \"Name ?\",employs[0].ename\n", + "print \"Existing date ?\",employs[0].epay\n", + "print \"Joinin date ?\",employs[0].jdate\n", + "print \n", + "print \"Press y- to continue any other key to stop. y\"\n", + "print \"Employee No. ?\",employs[1].eno\n", + "print \"Name ?\",employs[1].ename\n", + "print \"Existing date ?\",employs[1].epay\n", + "print \"Joinin date ?\",employs[1].jdate\n", + "print\n", + "print \"Press y- to continue any other key to stop. y\"\n", + "print \"Employee No. ?\",employs[2].eno\n", + "print \"Name ?\",employs[2].ename\n", + "print \"Existing date ?\",employs[2].epay\n", + "print \"Joinin date ?\",employs[2].jdate\n", + "print\n", + "print \"Press y- to continue any other key to stop. N\"\n", + "print\n", + "print n,\" records are entered\"\n", + "print \"Press any key to print the revised salary list\"\n", + "print \n", + "\n", + "\n", + "\n", + "def revise(temp):\n", + " if temp <= 2000:\n", + " temp = int(temp + math.ceil(temp * 0.15))\n", + " return temp\n", + " elif temp <= 5000:\n", + " temp = int(temp + temp * 0.10)\n", + " return temp\n", + " else:\n", + " return temp\n", + "\n", + " \n", + "# loop to increment salary\n", + "for i in range(n):\n", + " employs[i].epay = revise(employs[i].epay)\n", + "\n", + " \n", + "\n", + "\n", + "# loop to print revised salary list\n", + "print \" Employees Revised Pay List \"\n", + "print \"---------------------------------------------------------\"\n", + "print \" S.No. Number Name Joining date Pay \"\n", + "print \"---------------------------------------------------------\"\n", + "\n", + "for i in range(n):\n", + " print \" \",i+1,\" \",employs[i].eno,\" \",employs[i].ename,\" \",employs[i].jdate,\" \",employs[i].epay\n", + "\n", + "print \"---------------------------------------------------------\" \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Employee No. ? 20101\n", + "Name ? ASHIKA\n", + "Existing date ? 1000\n", + "Joinin date ? 31/04/2001\n", + "\n", + "Press y- to continue any other key to stop. y\n", + "Employee No. ? 20182\n", + "Name ? ASHWIN\n", + "Existing date ? 6000\n", + "Joinin date ? 11/12/1995\n", + "\n", + "Press y- to continue any other key to stop. y\n", + "Employee No. ? 20204\n", + "Name ? PRAVEEN\n", + "Existing date ? 3000\n", + "Joinin date ? 18/06/1994\n", + "\n", + "Press y- to continue any other key to stop. N\n", + "\n", + "3 records are entered\n", + "Press any key to print the revised salary list\n", + "\n", + " Employees Revised Pay List \n", + "---------------------------------------------------------\n", + " S.No. Number Name Joining date Pay \n", + "---------------------------------------------------------\n", + " 1 20101 ASHIKA 31/04/2001 1150\n", + " 2 20182 ASHWIN 11/12/1995 6000\n", + " 3 20204 PRAVEEN 18/06/1994 3300\n", + "---------------------------------------------------------\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 , Page number: CP-219" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to store cricket details and to display a team-wise list with batting average\n", + "\n", + "# class for cricket structure\n", + "\n", + "class cricket:\n", + " def __init__(self,pname,tname,bavg):\n", + " self.pname = pname\n", + " self.tname = tname\n", + " self.bavg = bavg\n", + "\n", + "n = 6\n", + "probable = []\n", + "probable.append(cricket(\"KUMBLE\",\"KARNATAKA\",22))\n", + "probable.append(cricket(\"KAMBLI\",\"MUMBAI\",39))\n", + "probable.append(cricket(\"SRIKANTH\",\"TAMILNADU\",52))\n", + "probable.append(cricket(\"SACHIM\",\"MUMBAI\",69))\n", + "probable.append(cricket(\"RAHUL\",\"KARNATAKA\",57))\n", + "probable.append(cricket(\"RAMESH\",\"TAMILNADU\",48))\n", + "\n", + "print \"How many players ?\",n\n", + "print\n", + "print \"Player name ?\",probable[0].pname\n", + "print \"Which team ?\",probable[0].tname\n", + "print \"Batting average ?\",probable[0].bavg\n", + "print \"Player name ?\",probable[1].pname\n", + "print \"Which team ?\",probable[1].tname\n", + "print \"Batting average ?\",probable[1].bavg\n", + "print \"Player name ?\",probable[2].pname\n", + "print \"Which team ?\",probable[2].tname\n", + "print \"Batting average ?\",probable[2].bavg\n", + "print \"Player name ?\",probable[3].pname\n", + "print \"Which team ?\",probable[3].tname\n", + "print \"Batting average ?\",probable[3].bavg\n", + "print \"Player name ?\",probable[4].pname\n", + "print \"Which team ?\",probable[4].tname\n", + "print \"Batting average ?\",probable[4].bavg\n", + "print \"Player name ?\",probable[5].pname\n", + "print \"Which team ?\",probable[5].tname\n", + "print \"Batting average ?\",probable[5].bavg\n", + "print\n", + "print \n", + "j = 0\n", + "teams = []\n", + "teams.append(probable[0].tname)\n", + "j = j + 1\n", + "for i in range(n):\n", + " flag = 0\n", + " for k in range(j):\n", + " if flag == 0:\n", + " if probable[i].tname == teams[k]:\n", + " flag = 1\n", + " if flag == 0 :\n", + " teams.append(probable[i].tname)\n", + " j = j + 1\n", + "\n", + "# loop to print team-wise list\n", + "\n", + "for k in range(3):\n", + " print \" \",teams[k]\n", + " print \"---------------------------------------------\"\n", + " for i in range(n):\n", + " if probable[i].tname == teams[k]:\n", + " print \" \",probable[i].pname,\" \",probable[i].bavg\n", + " print \"---------------------------------------------\" \n", + "\n", + "\n", + "\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many players ? 6\n", + "\n", + "Player name ? KUMBLE\n", + "Which team ? KARNATAKA\n", + "Batting average ? 22\n", + "Player name ? KAMBLI\n", + "Which team ? MUMBAI\n", + "Batting average ? 39\n", + "Player name ? SRIKANTH\n", + "Which team ? TAMILNADU\n", + "Batting average ? 52\n", + "Player name ? SACHIM\n", + "Which team ? MUMBAI\n", + "Batting average ? 69\n", + "Player name ? RAHUL\n", + "Which team ? KARNATAKA\n", + "Batting average ? 57\n", + "Player name ? RAMESH\n", + "Which team ? TAMILNADU\n", + "Batting average ? 48\n", + "\n", + "\n", + " KARNATAKA\n", + "---------------------------------------------\n", + " KUMBLE 22\n", + " RAHUL 57\n", + "---------------------------------------------\n", + " MUMBAI\n", + "---------------------------------------------\n", + " KAMBLI 39\n", + " SACHIM 69\n", + "---------------------------------------------\n", + " TAMILNADU\n", + "---------------------------------------------\n", + " SRIKANTH 52\n", + " RAMESH 48\n", + "---------------------------------------------\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 , Page number: CP-235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Program to illustrate the use of union using integer,string and float\n", + "\n", + "class student:\n", + " def __init__(self,roll_no,sname,marks):\n", + " self.roll_no = roll_no\n", + " self.sname = sname\n", + " self.marks = marks\n", + "\n", + "std = []\n", + "std.append(student(0,\"AJITH\",0))\n", + "std.append(student(0,\"RAJU\",0))\n", + "std.append(student(0,\"VIGNESH\",0))\n", + "std.append(student(0,\"DIVYA\",0))\n", + "\n", + "ch = 2\n", + "print \"-----------------------------\"\n", + "print \" Main menu \"\n", + "print \"-----------------------------\"\n", + "print \"Press 1 to enter roll numbers\"\n", + "print \" 2 to enter names \"\n", + "print \" 3 to enter marks \"\n", + "print \" 4 to stop \"\n", + "print \n", + "print \"Enter your choice :\",ch\n", + "\n", + "n = 4\n", + "print \"How many students?\",n\n", + "for i in range(n):\n", + " print \" Student name ? \",std[i].sname\n", + "\n", + "print\n", + "print \n", + "# display required list\n", + "# switch case\n", + "if ch == 1: #case 1\n", + " print \"------------------------------\"\n", + " print \" Students roll number list \"\n", + " print \"------------------------------\"\n", + " for i in range(n):\n", + " print std[i].roll_no\n", + " print \"-------------------------------\"\n", + "elif ch == 2: # case 2\n", + " print \"------------------------------\"\n", + " print \" Students name list \"\n", + " print \"------------------------------\"\n", + " for i in range(n):\n", + " print std[i].sname\n", + " print \"-------------------------------\"\n", + "elif ch == 3: # case 3\n", + " print \"------------------------------\"\n", + " print \" Students mark list \"\n", + " print \"------------------------------\"\n", + " for i in range(n):\n", + " print \"Student marks\",std[i].roll_no\n", + " print \"-------------------------------\"\n", + " \n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "-----------------------------\n", + " Main menu \n", + "-----------------------------\n", + "Press 1 to enter roll numbers\n", + " 2 to enter names \n", + " 3 to enter marks \n", + " 4 to stop \n", + "\n", + "Enter your choice : 2\n", + "How many students? 4\n", + " Student name ? AJITH\n", + " Student name ? RAJU\n", + " Student name ? VIGNESH\n", + " Student name ? DIVYA\n", + "\n", + "\n", + "------------------------------\n", + " Students name list \n", + "------------------------------\n", + "AJITH\n", + "RAJU\n", + "VIGNESH\n", + "DIVYA\n", + "-------------------------------\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Computer_Programming_Theory_and_Practice/screenshots/chapter11.png b/Computer_Programming_Theory_and_Practice/screenshots/chapter11.png Binary files differnew file mode 100755 index 00000000..c5895710 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/screenshots/chapter11.png diff --git a/Computer_Programming_Theory_and_Practice/screenshots/chapter2.png b/Computer_Programming_Theory_and_Practice/screenshots/chapter2.png Binary files differnew file mode 100755 index 00000000..05fde2b5 --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/screenshots/chapter2.png diff --git a/Computer_Programming_Theory_and_Practice/screenshots/chapter3.png b/Computer_Programming_Theory_and_Practice/screenshots/chapter3.png Binary files differnew file mode 100755 index 00000000..685e290b --- /dev/null +++ b/Computer_Programming_Theory_and_Practice/screenshots/chapter3.png |