summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-2.ipynb
blob: fc025445f55808476228cc116807fc00c2e9e4ea (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
{
 "metadata": {
  "name": "chapter-2.ipynb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 2: The Decision Control Structure <h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>If Demo , Page number: 52<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''Demonstration of if statement\n",
      "Here is a simple program, which demonstrates the use of if and the relational operators'''\n",
      "\n",
      "#taking in input from the user\n",
      "#num = raw_input(\"Enter a number less than 10: \")\n",
      "print \"Enter a number less than 10: \"\n",
      "num = 8\n",
      "print num\n",
      "\n",
      "#if statement\n",
      "if num <= 10:\n",
      "    print(\"What an obedient servant you are !\") #display result\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter a number less than 10: \n",
        "8\n",
        "What an obedient servant you are !\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.1 , Page number: 53<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000.\n",
      "If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.'''\n",
      "\n",
      "#Variable declaration\n",
      "dis = 0 #Initial Discount (%0)\n",
      "\n",
      "#Input from the user\n",
      "#qty,rate = raw_input(\"Enter quantity and rate: \").split()\n",
      "print \"Enter quantity and rate: \"\n",
      "qty = 1200  # Quantity of item\n",
      "rate = 15.50 # Rate of item (Rs)\n",
      "print qty , rate\n",
      "\n",
      "#discount of 10% if quantity > 1000\n",
      "if qty > 1000:\n",
      "    dis = 10\n",
      "\n",
      "#Calculation\n",
      "tot = (qty * rate) - (qty * rate * dis / 100 )  # total expenses (Rs)\n",
      "\n",
      "#Result\n",
      "print \"Total expenses = Rs. \", tot \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter quantity and rate: \n",
        "1200 15.5\n",
        "Total expenses = Rs.  16740.0\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.2, Page number: 57<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''The current year and the year in which the employee joined the organization are entered through the keyboard.\n",
      "If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee.\n",
      "If the years of service are not greater than 3, then the program should do nothing'''\n",
      "\n",
      "#input from user\n",
      "#cy,yoj = raw_input(\"Enter current year and year of joining: \").split() \n",
      "print \"Enter current year and year of joining: \"\n",
      "cy = 2013 # Current year\n",
      "yoj = 1990 # Year of joining\n",
      "print cy, yoj \n",
      "#Calculation\n",
      "yr_of_ser = cy - yoj # number of years of service\n",
      "\n",
      "#Assign bonus if years of service > 3\n",
      "if yr_of_ser > 3:\n",
      "    bonus = 2500 # Bonus of Rs. 2500\n",
      "    print \"Bonus = Rs.\", bonus  #display result\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter current year and year of joining: \n",
        "2013 1990\n",
        "Bonus = Rs. 2500\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.3 , Page number: 58<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''In a company an employee is paid as under:\n",
      "If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and\n",
      "DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary.\n",
      "If the employee's salary is input through the keyboard ,write a program to find his gross salary.'''\n",
      "\n",
      "#input from user\n",
      "#bs = raw_input(\"Enter basic salary: \")\n",
      "print \"Enter basic salary: \"\n",
      "bs = 2561.1 #Basic salary (Rs)\n",
      "print bs\n",
      "\n",
      "#Calculation\n",
      "if bs < 1500: # if basic salary is less than Rs.1500\n",
      "    hra = bs * 10 / 100 # HRA (Rs)\n",
      "    da = bs * 90 / 100 #DA (Rs)\n",
      "else: #if basic salary is greater than or equal to Rs.1500\n",
      "    hra = 500  # HRA (Rs)\n",
      "    da = bs * 98 / 100 # DA (Rs)\n",
      "\n",
      "gs = bs + hra + da # gross salary (Rs)\n",
      "\n",
      "#Result\n",
      "print \"gross salary = Rs. \", gs \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter basic salary: \n",
        "2561.1\n",
        "gross salary = Rs.  5570.978\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Nested If-else , Page number: 61<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''A quick demo of nested if-else'''\n",
      "\n",
      "#input from user\n",
      "#i = raw_input(\"Enter either 1 or 2: \")\n",
      "print \"Enter either 1 or 2: \"\n",
      "i = 1\n",
      "print i\n",
      "\n",
      "#nested if-else\n",
      "if i == 1 :\n",
      "    print \"You would go to heaven !\" \n",
      "else:\n",
      "    if i == 2 :\n",
      "        print \"Hell was created with you in mind\" \n",
      "    else:\n",
      "        print  \"How about mother earth !\" "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter either 1 or 2: \n",
        "1\n",
        "You would go to heaven !\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.4 (Method 1), Page number: 64<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:\n",
      "Percentage above or equal to 60 - First division\n",
      "Percentage between 50 and 59 - Second division\n",
      "Percentage between 40 and 49 - Third division\n",
      "Percentage less than 40 - Fail\n",
      "Write a program to calculate the division obtained by the student.\n",
      "Method 1'''\n",
      "\n",
      "#input from user\n",
      "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
      "print \"Enter marks in five subjects: \"\n",
      "m1 = 88 #Marks in 1st subject\n",
      "m2 = 92 #Marks in 2nd subject\n",
      "m3 = 87 #Marks in 3rd subject\n",
      "m4 = 66 #Marks in 4th subject\n",
      "m5 = 56 #Marks in 5th subject\n",
      "print m1,m2,m3,m4,m5\n",
      "\n",
      "#Calculation\n",
      "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
      "\n",
      "#check for different cases and display appropriate result\n",
      "if per >= 60:\n",
      "    print \"First division\"\n",
      "else:\n",
      "    if per >= 50:\n",
      "        print \"Second division\"\n",
      "    else:\n",
      "        if per >= 40:\n",
      "            print \"Third division\"\n",
      "        else:\n",
      "            print \"Fail\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter marks in five subjects: \n",
        "88 92 87 66 56\n",
        "First division\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.4 (Method 2), Page number: 65<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''Method 2'''\n",
      "\n",
      "#input from user\n",
      "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
      "print \"Enter marks in five subjects: \"\n",
      "m1 = 88 #Marks in 1st subject\n",
      "m2 = 92 #Marks in 2nd subject\n",
      "m3 = 87 #Marks in 3rd subject\n",
      "m4 = 66 #Marks in 4th subject\n",
      "m5 = 56 #Marks in 5th subject\n",
      "print m1,m2,m3,m4,m5\n",
      "\n",
      "#Calculation\n",
      "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
      "\n",
      "#check for different cases and display appropriate result\n",
      "if per >= 60:\n",
      "    print \"First division\"\n",
      "\n",
      "if (per >= 50) and (per <60):\n",
      "    print\"Second division\"\n",
      "\n",
      "if (per >= 40) and (per <50):\n",
      "    print\"Third division\"\n",
      "\n",
      "if per < 40 :\n",
      "    print\"Fail\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter marks in five subjects: \n",
        "88 92 87 66 56\n",
        "First division\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.4 (Method 3), Page number: 67<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''Method 3 - else if ladder demo'''\n",
      "\n",
      "#input from user\n",
      "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \n",
      "print \"Enter marks in five subjects: \"\n",
      "m1 = 88 #Marks in 1st subject\n",
      "m2 = 92 #Marks in 2nd subject\n",
      "m3 = 87 #Marks in 3rd subject\n",
      "m4 = 66 #Marks in 4th subject\n",
      "m5 = 56 #Marks in 5th subject\n",
      "print m1,m2,m3,m4,m5\n",
      "\n",
      "#Calculation\n",
      "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
      "\n",
      "#check for different cases and display appropriate result\n",
      "if per >= 60:\n",
      "    print\"First division\"\n",
      "elif per >= 50:\n",
      "    print\"Second division\"\n",
      "elif per >= 40:\n",
      "    print\"Third division\"\n",
      "else:\n",
      "    print\"Fail\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter marks in five subjects: \n",
        "88 92 87 66 56\n",
        "First division\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.5 (Method 1) , Page number: 68<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''A company insures its drivers in the following cases:\n",
      "\u2212 If the driver is married.\n",
      "\u2212 If the driver is unmarried, male & above 30 years of age.\n",
      "\u2212 If the driver is unmarried, female & above 25 years of age.\n",
      "In all other cases the driver is not insured.\n",
      "If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.'''\n",
      "\n",
      "#input from user\n",
      "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\n",
      "print \"Enter age, sex, marital status: \"\n",
      "age = 43 # Age of driver (years)\n",
      "sex = 'M'\n",
      "ms = 'M'\n",
      "print age,sex,ms\n",
      "#check for different cases and display appropriate result\n",
      "if ms == 'M':\n",
      "    print(\"Driver is insured\")\n",
      "else:\n",
      "    if sex == 'M':\n",
      "        if age > 30:\n",
      "            print (\"Driver is insured\")\n",
      "        else:\n",
      "            print (\"Driver is not insured\")\n",
      "    else:\n",
      "        if age > 25:\n",
      "            print (\"Driver is insured\")\n",
      "        else:\n",
      "            print (\"Driver is not insured\")\n",
      "                \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter age, sex, marital status: \n",
        "43 M M\n",
        "Driver is insured\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.5 (Method 2) , Page number: 69<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''Using logical operators'''\n",
      "\n",
      "#input from user\n",
      "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\n",
      "print \"Enter age, sex, marital status: \"\n",
      "age = 43 # Age of driver (years)\n",
      "sex = 'M'\n",
      "ms = 'M'\n",
      "print age,sex,ms\n",
      "\n",
      "#check for different cases and display appropriate result\n",
      "if ((ms == 'M') or (ms == 'U' and sex == 'M' and age > 30) or (ms == 'U' and sex == 'F' and age >25) ) :\n",
      "    print\"Driver is insured\"\n",
      "else:\n",
      "    print\"Driver is not insured\"\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter age, sex, marital status: \n",
        "43 M M\n",
        "Driver is insured\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.6, Page number: 71<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''Write a program to calculate the salary as per the given table'''\n",
      "\n",
      "#Input gender( m/f), years of service and qualification from the user\n",
      "#g,yos,qual = raw_input(\"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\").split()\n",
      "print \"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\"\n",
      "g = 'f'\n",
      "yos = 8 # Years of service(years)\n",
      "qual = 1 # Qualification ( 0=G, 1=PG)\n",
      "print g,yos,qual\n",
      "\n",
      "# Assign salary depending upon the conditions\n",
      "if (g == 'm') and (yos >= 10) and (qual == 1):\n",
      "    sal = 15000 #salary\n",
      "elif ((g == 'm' and yos >= 10 and qual == 0) or ( g == 'm' and yos < 10 and qual == 1 )):\n",
      "    sal = 10000 #salary\n",
      "elif ( g == 'm' and yos < 10 and qual == 0 ):\n",
      "    sal = 7000  #salary\n",
      "elif ( g == 'f' and yos >= 10 and qual == 1 ):\n",
      "    sal = 12000 #salary\n",
      "elif ( g == 'f' and yos >= 10 and qual == 0 ):\n",
      "    sal = 9000  #salary\n",
      "elif ( g == 'f' and yos < 10 and qual == 1 ):\n",
      "    sal = 10000 #salary\n",
      "elif ( g == 'f' and yos < 10 and qual == 0 ):\n",
      "    sal = 6000  #salary\n",
      "\n",
      "#Result\n",
      "print  \"Salary of Employee = \", sal "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\n",
        "f 8 1\n",
        "Salary of Employee =  10000\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}