summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_4(1).ipynb
blob: ebd72abc31927ce589ecbb16278b50652f22c374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
{
 "metadata": {
  "name": "",
  "signature": "sha256:7a7e07f5e71ec203dfa93cc13b4ce9de80c80af3b59f8d7d6bebe41c247db83e"
 },
 "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",
      "\n",
      "#Displaying the menu\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",
      "'''Displays all printable characters.'''\n",
      "\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 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",
        "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",
        "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",
        "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",
        "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",
        "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",
        "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",
        "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": {}
  }
 ]
}