diff options
Diffstat (limited to 'ANSI_C_Programming/chapter2.ipynb')
-rw-r--r-- | ANSI_C_Programming/chapter2.ipynb | 782 |
1 files changed, 782 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter2.ipynb b/ANSI_C_Programming/chapter2.ipynb new file mode 100644 index 00000000..255326b2 --- /dev/null +++ b/ANSI_C_Programming/chapter2.ipynb @@ -0,0 +1,782 @@ +{
+ "metadata": {
+ "name": "chapter2.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 2: THE DECISION CONTROL STRUCTURE"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE-45"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Demonstration of if statement\n",
+ "print \"Enter a number less than 10\"\n",
+ "num=eval(raw_input()) #Inputs float variables #you can also input float variables using input()\n",
+ "if num<10:\n",
+ " print \"What an obedient servant you are !\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number less than 10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What an obedient servant you are !\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1: Page-47"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculation of total expenses\n",
+ "print \"Enter quantity and rate\"\n",
+ "qty=eval(raw_input()) #Inputs float variables\n",
+ "rate=eval(raw_input())\n",
+ "dis=0\n",
+ "if qty>1000:\n",
+ " dis=10\n",
+ "tot=(qty*rate)-(qty*rate*dis/100)\n",
+ "print \"Total expenses=Rs.\" ,tot"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter quantity and rate\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1200\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "15.50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Total expenses=Rs. 16740.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.2: Page-49"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculation of bonus\n",
+ "print \"Enter current year and year of joining\"\n",
+ "cy=eval(raw_input())\n",
+ "yoj=eval(raw_input())\n",
+ "yos=cy-yoj\n",
+ "if yos>3:\n",
+ " bonus=2500\n",
+ " print \"Bonus=Rs.\" ,bonus"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter current year and year of joining\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2014\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2009\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Bonus=Rs. 2500\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.3: Page-51"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculation of gross salary\n",
+ "print \"Enter basic salary\"\n",
+ "bs=eval(raw_input())\n",
+ "if bs<1500:\n",
+ " hra=bs*10/100\n",
+ " da=bs*90/100\n",
+ "else:\n",
+ " hra=500\n",
+ " da=bs*98/100\n",
+ "gs=bs+hra+da\n",
+ "print \"gross salary=Rs.\" ,gs"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter basic salary\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1000\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "gross salary=Rs. 2000\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON Page-53"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#A quick demo of nested if-else\n",
+ "print \"Enter either 1 or 2\"\n",
+ "i=eval(raw_input())\n",
+ "if i==1:\n",
+ " print \"You would go to heaven !\\n\"\n",
+ "else:\n",
+ " if i==2:\n",
+ " print \"Hell was created with you in mind\\n\"\n",
+ " else:\n",
+ " print \"How about mother earth !\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter either 1 or 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How about mother earth !\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.4: Page-55-56"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Method-I: program using nested if-else to find division of student\n",
+ "print \"Enter marks in five subjects\"\n",
+ "m1=eval(raw_input())\n",
+ "m2=eval(raw_input())\n",
+ "m3=eval(raw_input())\n",
+ "m4=eval(raw_input())\n",
+ "m5=eval(raw_input())\n",
+ "per=(m1+m2+m3+m4+m5)*100/500\n",
+ "if per>=60:\n",
+ " print \"First division\\n\"\n",
+ "else:\n",
+ " if per>=50:\n",
+ " print \"Second division\\n\"\n",
+ " else:\n",
+ " if per>=40:\n",
+ " print \"Third division\\n\"\n",
+ " else:\n",
+ " print \"Fail\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks in five subjects\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "87\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "98\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First division\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON Page-56-57"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Method-II: program using if statement to find division of student\n",
+ "print \"Enter marks in five subjects\"\n",
+ "m1=eval(raw_input())\n",
+ "m2=eval(raw_input())\n",
+ "m3=eval(raw_input())\n",
+ "m4=eval(raw_input())\n",
+ "m5=eval(raw_input())\n",
+ "per=(m1+m2+m3+m4+m5)*100/500\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"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "87\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "98\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First division\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON Page-58"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#else if ladder demo\n",
+ "print \"Enter marks in five subjects\"\n",
+ "m1=eval(raw_input())\n",
+ "m2=eval(raw_input())\n",
+ "m3=eval(raw_input())\n",
+ "m4=eval(raw_input())\n",
+ "m5=eval(raw_input())\n",
+ "per=(m1+m2+m3+m4+m5)*100/500\n",
+ "if per>=60:\n",
+ " print \"First division\\n\"\n",
+ "elif per>=50:\n",
+ " print \"Second division\\n\"\n",
+ "elif per>=40:\n",
+ " print \"Third division\\n\"\n",
+ "else:\n",
+ " print \"fail\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter marks in five subjects\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "70\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "87\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "91\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "98\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First division\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.5: Page-59"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Insurance of driver-without using logical operators\n",
+ "print \"Enter age,sex,marital status\"\n",
+ "age=eval(raw_input())\n",
+ "sex=raw_input() #Inputs string variables i,e; we can't use these variables in calculations\n",
+ "ms=raw_input() #Inputs string variables\n",
+ "if ms=='M':\n",
+ " print \"Driver is insured\\n\"\n",
+ "else:\n",
+ " if sex=='M':\n",
+ " if age>30:\n",
+ " print \"Driver is insured\\n\"\n",
+ " else:\n",
+ " print \"Driver is not insured\\n\"\n",
+ " else:\n",
+ " if age>25:\n",
+ " print \"Driver is insured\\n\"\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"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "M\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "U\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Driver is not insured\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON Page-60"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Insurance of driver-using logical operators\n",
+ "print \"Enter age,sex,marital status\"\n",
+ "age=eval(raw_input())\n",
+ "sex=raw_input()\n",
+ "ms=raw_input()\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\"\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"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "M\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "U\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Driver is not insured\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.6: Page-61-62"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculate the salary\n",
+ "print \"Enter Gender,Years of Service and Qualifications(0=G,1=PG):\"\n",
+ "g=raw_input()\n",
+ "yos=eval(raw_input())\n",
+ "qual=eval(raw_input())\n",
+ "sal=0\n",
+ "if g=='m' and yos>=10 and qual==1:\n",
+ " sal=15000\n",
+ "elif (g=='m' and yos>=10 and qual==0) or (g=='m' and yos<10 and qual==1):\n",
+ " sal=10000\n",
+ "elif g=='m' and yos<10 and qual==0:\n",
+ " sal=7000\n",
+ "elif g=='f' and yos>=10 and qual==1:\n",
+ " sal=12000\n",
+ "elif g=='f' and yos>=10 and qual==0:\n",
+ " sal=9000\n",
+ "elif g=='f' and yos<10 and qual==1:\n",
+ " sal=10000\n",
+ "elif g=='f' and yos<10 and qual==0:\n",
+ " sal=6000\n",
+ "print \"\\nSalary 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"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Salary of Employee= 7000\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |