summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_4.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_4.ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_4.ipynb1340
1 files changed, 1340 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_4.ipynb b/C++_from_the_Ground/Chapter_4.ipynb
new file mode 100644
index 00000000..ca8e76db
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_4.ipynb
@@ -0,0 +1,1340 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:f944500666c92742656b5a25638e93a82042acafb19b205aac684e2d9ac2df51"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 4: Program Control Statements<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.1, Page Number: 58<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable declaration and initialization\n",
+ "magic = random.randint(0,100) #Number which the user has to guess\n",
+ "guess = 10 #Number which the user guesses\n",
+ "\n",
+ "if guess == magic:\n",
+ " print \"***Right***\" #Result\n",
+ "\n",
+ "\n",
+ " \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.2, Page Number: 59<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable decleration and initialization\n",
+ "magic=random.randint(0,100) #Number to be guessed\n",
+ "guess = 10 #Number the user guesses\n",
+ "\n",
+ "#Result\n",
+ "if guess == magic:\n",
+ " print \"***Right***\"\n",
+ "else:\n",
+ " print \"... Sorry, you're wrong.\"\n",
+ "\n",
+ "\n",
+ "\n",
+ " \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "... Sorry, you're wrong.\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.3, Page Number: 60<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable decleration and initialization\n",
+ "a = 30 #User input for the two nos.\n",
+ "b = 10\n",
+ "\n",
+ "#Calculation and Result\n",
+ "if b:\n",
+ " print a/b\n",
+ "else:\n",
+ " print \"Cannot divide by zero.\"\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.4, Page Number: 61<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable decleration \n",
+ "magic = random.randint(0,100) #Numbr to be guessed\n",
+ "guess = 10 #Number the user guesses\n",
+ "\n",
+ "#Result\n",
+ "if guess == magic:\n",
+ " print \"***Right***\"\n",
+ " print magic,\" is the magic number.\"\n",
+ "else:\n",
+ " print \"... Sorry, you're wrong\" \n",
+ " if(guess>magic): #use a nested if statement\n",
+ " print \"Your guess is too high\"\n",
+ " else:\n",
+ " print \"Your guess is too low\"\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.5, Page Number: 62<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "for x in range(6):\n",
+ " if x==1: #using if-else ladder in a for loop\n",
+ " print \"x is one\"\n",
+ " elif x==2:\n",
+ " print \"x is two\"\n",
+ " elif x==3:\n",
+ " print \"x is three\"\n",
+ " elif x==4:\n",
+ " print \"x is four\"\n",
+ " else:\n",
+ " print \"x is not between 1 nd 4\"\n",
+ " \n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x is not between 1 nd 4\n",
+ "x is one\n",
+ "x is two\n",
+ "x is three\n",
+ "x is four\n",
+ "x is not between 1 nd 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.6, Page Number: 63<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import math\n",
+ "\n",
+ "for num in range(100):\n",
+ " sq_root = math.sqrt(float(num)) #Calculation\n",
+ " print num,\" \",sq_root #Result\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 0.0\n",
+ "1 1.0\n",
+ "2 1.41421356237\n",
+ "3 1.73205080757\n",
+ "4 2.0\n",
+ "5 2.2360679775\n",
+ "6 2.44948974278\n",
+ "7 2.64575131106\n",
+ "8 2.82842712475\n",
+ "9 3.0\n",
+ "10 3.16227766017\n",
+ "11 3.31662479036\n",
+ "12 3.46410161514\n",
+ "13 3.60555127546\n",
+ "14 3.74165738677\n",
+ "15 3.87298334621\n",
+ "16 4.0\n",
+ "17 4.12310562562\n",
+ "18 4.24264068712\n",
+ "19 4.35889894354\n",
+ "20 4.472135955\n",
+ "21 4.58257569496\n",
+ "22 4.69041575982\n",
+ "23 4.79583152331\n",
+ "24 4.89897948557\n",
+ "25 5.0\n",
+ "26 5.09901951359\n",
+ "27 5.19615242271\n",
+ "28 5.29150262213\n",
+ "29 5.38516480713\n",
+ "30 5.47722557505\n",
+ "31 5.56776436283\n",
+ "32 5.65685424949\n",
+ "33 5.74456264654\n",
+ "34 5.83095189485\n",
+ "35 5.9160797831\n",
+ "36 6.0\n",
+ "37 6.0827625303\n",
+ "38 6.16441400297\n",
+ "39 6.2449979984\n",
+ "40 6.32455532034\n",
+ "41 6.40312423743\n",
+ "42 6.48074069841\n",
+ "43 6.5574385243\n",
+ "44 6.63324958071\n",
+ "45 6.7082039325\n",
+ "46 6.78232998313\n",
+ "47 6.8556546004\n",
+ "48 6.92820323028\n",
+ "49 7.0\n",
+ "50 7.07106781187\n",
+ "51 7.14142842854\n",
+ "52 7.21110255093\n",
+ "53 7.28010988928\n",
+ "54 7.34846922835\n",
+ "55 7.4161984871\n",
+ "56 7.48331477355\n",
+ "57 7.54983443527\n",
+ "58 7.61577310586\n",
+ "59 7.68114574787\n",
+ "60 7.74596669241\n",
+ "61 7.81024967591\n",
+ "62 7.87400787401\n",
+ "63 7.93725393319\n",
+ "64 8.0\n",
+ "65 8.0622577483\n",
+ "66 8.12403840464\n",
+ "67 8.18535277187\n",
+ "68 8.24621125124\n",
+ "69 8.30662386292\n",
+ "70 8.36660026534\n",
+ "71 8.42614977318\n",
+ "72 8.48528137424\n",
+ "73 8.54400374532\n",
+ "74 8.60232526704\n",
+ "75 8.66025403784\n",
+ "76 8.71779788708\n",
+ "77 8.77496438739\n",
+ "78 8.83176086633\n",
+ "79 8.88819441732\n",
+ "80 8.94427191\n",
+ "81 9.0\n",
+ "82 9.05538513814\n",
+ "83 9.11043357914\n",
+ "84 9.16515138991\n",
+ "85 9.21954445729\n",
+ "86 9.2736184955\n",
+ "87 9.32737905309\n",
+ "88 9.38083151965\n",
+ "89 9.43398113206\n",
+ "90 9.48683298051\n",
+ "91 9.53939201417\n",
+ "92 9.59166304663\n",
+ "93 9.64365076099\n",
+ "94 9.69535971483\n",
+ "95 9.74679434481\n",
+ "96 9.79795897113\n",
+ "97 9.8488578018\n",
+ "98 9.89949493661\n",
+ "99 9.94987437107\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.7, Page Number: 64<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for i in xrange(100,-100,-5): \n",
+ " print i,\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45 -50 -55 -60 -65 -70 -75 -80 -85 -90 -95\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.8, Page Number: 66<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "#Variable decleration\n",
+ "x=0\n",
+ "\n",
+ "#Loop till 123 is entered\n",
+ "while x!=123:\n",
+ " print \"Enter a number: \"\n",
+ " x = 123\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.9, Page Number: 68<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "print \"Help on:\"\n",
+ "print \"1. for\"\n",
+ "print \"2. if\"\n",
+ "print \"3. while\"\n",
+ "\n",
+ "\n",
+ "choice = 2 #Choice of user\n",
+ "\n",
+ "if choice==1: #Executing users choice with if-else\n",
+ " print \"for is c++'s most versatile loop.\"\n",
+ "elif choice==2:\n",
+ " print \"if is c++'s conditional branch statement.\"\n",
+ "elif choice==3:\n",
+ " print \"switch is C++'s multiway branch statement. \"\n",
+ "else:\n",
+ " print \"You must enter a number between 1 and 3.\"\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Help on:\n",
+ "1. for\n",
+ "2. if\n",
+ "3. while\n",
+ "if is c++'s conditional branch statement.\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.10, Page Number: 69<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for i in range(5):\n",
+ " if i==0:\n",
+ " print \"less than 1\"\n",
+ " elif i==1:\n",
+ " print \"less than 2\"\n",
+ " elif i==2:\n",
+ " print \"less than 3\"\n",
+ " elif i==3:\n",
+ " print \"less than 4\"\n",
+ " elif i==4:\n",
+ " print \"less than 5\"\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "less than 1\n",
+ "less than 2\n",
+ "less than 3\n",
+ "less than 4\n",
+ "less than 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.11, Page Number: 71<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ " \n",
+ "#Variable decleration\n",
+ "ch = 32\n",
+ "\n",
+ "#Loop to print the characters\n",
+ "for ch in range(128):\n",
+ " print chr(ch)\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\u0000\n",
+ "\u0001\n",
+ "\u0002\n",
+ "\u0003\n",
+ "\u0004\n",
+ "\u0005\n",
+ "\u0006\n",
+ "\u0007\n",
+ "\b\n",
+ "\t\n",
+ "\n",
+ "\n",
+ "\u000b",
+ "\n",
+ "\f",
+ "\n",
+ "\r\n",
+ "\u000e\n",
+ "\u000f\n",
+ "\u0010\n",
+ "\u0011\n",
+ "\u0012\n",
+ "\u0013\n",
+ "\u0014\n",
+ "\u0015\n",
+ "\u0016\n",
+ "\u0017\n",
+ "\u0018\n",
+ "\u0019\n",
+ "\u001a\n",
+ "\u001b\n",
+ "\u001c",
+ "\n",
+ "\u001d",
+ "\n",
+ "\u001e",
+ "\n",
+ "\u001f\n",
+ " \n",
+ "!\n",
+ "\"\n",
+ "#\n",
+ "$\n",
+ "%\n",
+ "&\n",
+ "'\n",
+ "(\n",
+ ")\n",
+ "*\n",
+ "+\n",
+ ",\n",
+ "-\n",
+ ".\n",
+ "/\n",
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n",
+ ":\n",
+ ";\n",
+ "<\n",
+ "=\n",
+ ">\n",
+ "?\n",
+ "@\n",
+ "A\n",
+ "B\n",
+ "C\n",
+ "D\n",
+ "E\n",
+ "F\n",
+ "G\n",
+ "H\n",
+ "I\n",
+ "J\n",
+ "K\n",
+ "L\n",
+ "M\n",
+ "N\n",
+ "O\n",
+ "P\n",
+ "Q\n",
+ "R\n",
+ "S\n",
+ "T\n",
+ "U\n",
+ "V\n",
+ "W\n",
+ "X\n",
+ "Y\n",
+ "Z\n",
+ "[\n",
+ "\\\n",
+ "]\n",
+ "^\n",
+ "_\n",
+ "`\n",
+ "a\n",
+ "b\n",
+ "c\n",
+ "d\n",
+ "e\n",
+ "f\n",
+ "g\n",
+ "h\n",
+ "i\n",
+ "j\n",
+ "k\n",
+ "l\n",
+ "m\n",
+ "n\n",
+ "o\n",
+ "p\n",
+ "q\n",
+ "r\n",
+ "s\n",
+ "t\n",
+ "u\n",
+ "v\n",
+ "w\n",
+ "x\n",
+ "y\n",
+ "z\n",
+ "{\n",
+ "|\n",
+ "}\n",
+ "~\n",
+ "\u007f\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.12, Page Number: 72<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable decleration\n",
+ "len = 5\n",
+ "\n",
+ "while (len>0 & len<80):\n",
+ " print \".\"\n",
+ " len-=1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ ".\n",
+ ".\n",
+ ".\n",
+ ".\n",
+ ".\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.13, Page Number: 73<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable Declaration\n",
+ "num=95\n",
+ "\n",
+ "while True:\n",
+ " print \"Enter a number(100 to stop): \"\n",
+ " num+=1 #User input, incrementing num till it reaches 100\n",
+ " if(num==100): #Condition check to stop loop\n",
+ " break\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number(100 to stop): \n",
+ "Enter a number(100 to stop): \n",
+ "Enter a number(100 to stop): \n",
+ "Enter a number(100 to stop): \n",
+ "Enter a number(100 to stop): \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.14, Page Number: 73<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable decleration\n",
+ "magic = random.randint(0,100) #Number which the user has to guess\n",
+ "low=0\n",
+ "high=100\n",
+ "\n",
+ "#Play thr magic number game\n",
+ "while True:\n",
+ " guess = random.randint(low,high) #Number which the user guesses\n",
+ " if guess==magic: \n",
+ " print \"**Right**\"\n",
+ " print magic,\" is the magic number.\"\n",
+ " break\n",
+ " else:\n",
+ " print \"...Sorry, you're wrong.\"\n",
+ " if(guess>magic):\n",
+ " print \"Your guess is too high.\"\n",
+ " high=guess\n",
+ " else:\n",
+ " print \"Your guess is too low.\"\n",
+ " low=guess\n",
+ " \n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too high.\n",
+ "...Sorry, you're wrong.\n",
+ "Your guess is too low.\n",
+ "**Right**\n",
+ "72 is the magic number.\n"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.15, Page Number: 74<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for x in range(100+1):\n",
+ " if x%2: #Condition check to continue the loop\n",
+ " continue\n",
+ " print x,\n",
+ " \n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.16, Page Number: 75<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for t in range(100):\n",
+ " if t==10: #Condition check to break out of the loop\n",
+ " break\n",
+ " print t,\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 1 2 3 4 5 6 7 8 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.17, Page Number: 75<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for t in range(100):\n",
+ " count = 1 \n",
+ " while True:\n",
+ " print count,\n",
+ " count+=1\n",
+ " if count==10: \n",
+ " break #Breaks from the inner loop\n",
+ " print\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2 3 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8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n",
+ "1 2 3 4 5 6 7 8 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.18, Page Number: 76<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "for i in range(2,1000):\n",
+ " j=2\n",
+ " for j in range(2,i/j): #Nested for loop\n",
+ " if i%j == False: #Check for prime no.\n",
+ " break\n",
+ " if j>(i/j):\n",
+ " print i,\" is prime.\" #Result\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 is prime.\n",
+ "3 is prime.\n",
+ "11 is prime.\n",
+ "13 is prime.\n",
+ "17 is prime.\n",
+ "19 is prime.\n",
+ "23 is prime.\n",
+ "29 is prime.\n",
+ "31 is prime.\n",
+ "37 is prime.\n",
+ "41 is prime.\n",
+ "43 is prime.\n",
+ "47 is prime.\n",
+ "53 is prime.\n",
+ "59 is prime.\n",
+ "61 is prime.\n",
+ "67 is prime.\n",
+ "71 is prime.\n",
+ "73 is prime.\n",
+ "79 is prime.\n",
+ "83 is prime.\n",
+ "89 is prime.\n",
+ "97 is prime.\n",
+ "101 is prime.\n",
+ "103 is prime.\n",
+ "107 is prime.\n",
+ "109 is prime.\n",
+ "113 is prime.\n",
+ "127 is prime.\n",
+ "131 is prime.\n",
+ "137 is prime.\n",
+ "139 is prime.\n",
+ "149 is prime.\n",
+ "151 is prime.\n",
+ "157 is prime.\n",
+ "163 is prime.\n",
+ "167 is prime.\n",
+ "173 is prime.\n",
+ "179 is prime.\n",
+ "181 is prime.\n",
+ "191 is prime.\n",
+ "193 is prime.\n",
+ "197 is prime.\n",
+ "199 is prime.\n",
+ "211 is prime.\n",
+ "223 is prime.\n",
+ "227 is prime.\n",
+ "229 is prime.\n",
+ "233 is prime.\n",
+ "239 is prime.\n",
+ "241 is prime.\n",
+ "251 is prime.\n",
+ "257 is prime.\n",
+ "263 is prime.\n",
+ "269 is prime.\n",
+ "271 is prime.\n",
+ "277 is prime.\n",
+ "281 is prime.\n",
+ "283 is prime.\n",
+ "293 is prime.\n",
+ "307 is prime.\n",
+ "311 is prime.\n",
+ "313 is prime.\n",
+ "317 is prime.\n",
+ "331 is prime.\n",
+ "337 is prime.\n",
+ "347 is prime.\n",
+ "349 is prime.\n",
+ "353 is prime.\n",
+ "359 is prime.\n",
+ "367 is prime.\n",
+ "373 is prime.\n",
+ "379 is prime.\n",
+ "383 is prime.\n",
+ "389 is prime.\n",
+ "397 is prime.\n",
+ "401 is prime.\n",
+ "409 is prime.\n",
+ "419 is prime.\n",
+ "421 is prime.\n",
+ "431 is prime.\n",
+ "433 is prime.\n",
+ "439 is prime.\n",
+ "443 is prime.\n",
+ "449 is prime.\n",
+ "457 is prime.\n",
+ "461 is prime.\n",
+ "463 is prime.\n",
+ "467 is prime.\n",
+ "479 is prime.\n",
+ "487 is prime.\n",
+ "491 is prime.\n",
+ "499 is prime.\n",
+ "503 is prime.\n",
+ "509 is prime.\n",
+ "521 is prime.\n",
+ "523 is prime.\n",
+ "541 is prime.\n",
+ "547 is prime.\n",
+ "557 is prime.\n",
+ "563 is prime.\n",
+ "569 is prime.\n",
+ "571 is prime.\n",
+ "577 is prime.\n",
+ "587 is prime.\n",
+ "593 is prime.\n",
+ "599 is prime.\n",
+ "601 is prime.\n",
+ "607 is prime.\n",
+ "613 is prime.\n",
+ "617 is prime.\n",
+ "619 is prime.\n",
+ "631 is prime.\n",
+ "641 is prime.\n",
+ "643 is prime.\n",
+ "647 is prime.\n",
+ "653 is prime.\n",
+ "659 is prime.\n",
+ "661 is prime.\n",
+ "673 is prime.\n",
+ "677 is prime.\n",
+ "683 is prime.\n",
+ "691 is prime.\n",
+ "701 is prime.\n",
+ "709 is prime.\n",
+ "719 is prime.\n",
+ "727 is prime.\n",
+ "733 is prime.\n",
+ "739 is prime.\n",
+ "743 is prime.\n",
+ "751 is prime.\n",
+ "757 is prime.\n",
+ "761 is prime.\n",
+ "769 is prime.\n",
+ "773 is prime.\n",
+ "787 is prime.\n",
+ "797 is prime.\n",
+ "809 is prime.\n",
+ "811 is prime.\n",
+ "821 is prime.\n",
+ "823 is prime.\n",
+ "827 is prime.\n",
+ "829 is prime.\n",
+ "839 is prime.\n",
+ "853 is prime.\n",
+ "857 is prime.\n",
+ "859 is prime.\n",
+ "863 is prime.\n",
+ "877"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " is prime.\n",
+ "881 is prime.\n",
+ "883 is prime.\n",
+ "887 is prime.\n",
+ "907 is prime.\n",
+ "911 is prime.\n",
+ "919 is prime.\n",
+ "929 is prime.\n",
+ "937 is prime.\n",
+ "941 is prime.\n",
+ "947 is prime.\n",
+ "953 is prime.\n",
+ "967 is prime.\n",
+ "971 is prime.\n",
+ "977 is prime.\n",
+ "983 is prime.\n",
+ "991 is prime.\n",
+ "997 is prime.\n"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 4.19, Page Number: 78<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import random\n",
+ "\n",
+ "#Variable declaration\n",
+ "magic = random.randint(0,100) #Number to be guessed\n",
+ "i=1\n",
+ "high=100\n",
+ "low=0\n",
+ "\n",
+ "#Function to play the magic number game\n",
+ "def play(m):\n",
+ " low=0\n",
+ " high=100\n",
+ " for t in range(100):\n",
+ " x = random.randint(low,high) #Number guessed by the user\n",
+ " if x==m:\n",
+ " print \"***Right***\"\n",
+ " return\n",
+ " else:\n",
+ " if(x<m):\n",
+ " print \"Too low.\"\n",
+ " low=x\n",
+ " else:\n",
+ " print \"Too high.\"\n",
+ " high=x\n",
+ " print \"You've used up all your guesses\" \n",
+ "\n",
+ "#Menu\n",
+ "while True:\n",
+ " print \"1.Get a new magic number.\" \n",
+ " print \"2.Play\"\n",
+ " print \"3.Quit\"\n",
+ " while True:\n",
+ " option = i \n",
+ " if option>=1 and option<=3:\n",
+ " break\n",
+ " if option==1:\n",
+ " magic=random.randint(0,100) #Number to be guessed\n",
+ " elif option==2:\n",
+ " play(magic) #Calls the function play\n",
+ " elif option==3:\n",
+ " print \"Goodbye\" \n",
+ " break\n",
+ " i+=1 #increments i such that the 3 options get selected sequentially\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1.Get a new magic number.\n",
+ "2.Play\n",
+ "3.Quit\n",
+ "1.Get a new magic number.\n",
+ "2.Play\n",
+ "3.Quit\n",
+ "Too low.\n",
+ "Too low.\n",
+ "Too low.\n",
+ "Too low.\n",
+ "Too high.\n",
+ "Too low.\n",
+ "Too high.\n",
+ "Too low.\n",
+ "Too high.\n",
+ "Too high.\n",
+ "Too high.\n",
+ "Too high.\n",
+ "Too low.\n",
+ "***Right***\n",
+ "1.Get a new magic number.\n",
+ "2.Play\n",
+ "3.Quit\n",
+ "Goodbye\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file