diff options
Diffstat (limited to 'Let_us_C/chapter-2.ipynb')
-rw-r--r-- | Let_us_C/chapter-2.ipynb | 571 |
1 files changed, 571 insertions, 0 deletions
diff --git a/Let_us_C/chapter-2.ipynb b/Let_us_C/chapter-2.ipynb new file mode 100644 index 00000000..fc025445 --- /dev/null +++ b/Let_us_C/chapter-2.ipynb @@ -0,0 +1,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": {}
+ }
+ ]
+}
\ No newline at end of file |