{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5: Control Statements in C" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1, Page Number : CSC-3" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"\\nEnter two numbers : \"))\n", "b = int(raw_input(\"\"))\n", "big = a\n", "\n", "#Processing\n", "if b > big:\n", " big = b\n", "\n", "#Output\n", "print \"\\nBiggest number is \",big\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter two numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Biggest number is 8\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, Page Number : CSC-4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"\\nEnter three numbers : \"))\n", "b = int(raw_input(\"\"))\n", "c = int(raw_input(\"\"))\n", "big = a\n", "\n", "#Processing\n", "if b > big:\n", " big = b\n", " \n", "if c > big:\n", " big = c\n", " \n", "#Output\n", "print \"\\nBiggest number is \",big" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter three numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "13\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Biggest number is 13\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, Page Number : CSC-6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"\\nEnter three numbers : \"))\n", "b = int(raw_input(\"\"))\n", "c = int(raw_input(\"\"))\n", "\n", "#Processing\n", "if a > b:\n", " if a > c:\n", " big = a\n", " else:\n", " big = c\n", "else:\n", " if b > c:\n", " big = b\n", " else:\n", " big = c\n", " \n", "#Output\n", "print \"\\nBiggest number is \",big" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter three numbers : 18\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "13\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Biggest number is 18\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4, Page Number : CSC-7" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "x = float(raw_input(\"\\nEnter value to x and n : \"))\n", "n = int(raw_input(\"\"))\n", "\n", "if n == 1:\n", " y = 1 + x\n", "else:\n", " if n == 2:\n", " y = 1 + x/n\n", " else:\n", " if n == 3:\n", " y = 1 + pow(x,n)\n", " else:\n", " y = 1 + n*x\n", " \n", "print \"\\nValue of y(x,n) = \",y" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to x and n : 0.42\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Value of y(x,n) = 3.1\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5, Page Number : CSC-9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "x = float(raw_input(\"\\nEnter value to x and n : \"))\n", "n = int(raw_input(\"\"))\n", "\n", "#There is no switch statement in python\n", "if n == 1:\n", " y = 1 + x\n", "else:\n", " if n == 2:\n", " y = 1 + x/n\n", " else:\n", " if n == 3:\n", " y = 1 + pow(x,n)\n", " else:\n", " y = 1 + n*x\n", " \n", "sys.stdout.write(\"\\nValue of y(x,n) = %6.2f\"%(y))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter value to x and n : 0.42\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Value of y(x,n) = 3.10" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6, Page Number : CSC-11" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "sales = int(raw_input(\"\\nSales amount ? \"))\n", "\n", "if sales <= 500:\n", " comm = 0.05 * sales\n", "else:\n", " if sales <= 2000:\n", " comm = 35 + 0.10 * (sales - 500)\n", " else:\n", " if sales <= 5000:\n", " comm = 185 + 0.12 * (sales - 2000)\n", " else:\n", " comm = 0.125 * sales\n", " \n", "sys.stdout.write(\"\\nCommission Amount Rs. %0.2f\"%(comm))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sales amount ? 4500\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Commission Amount Rs. 485.00" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, Page Number : CSC-12" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import math\n", "\n", "a = int(raw_input(\"\\nEnter coefficients a,b and c : \"))\n", "b = int(raw_input())\n", "c = int(raw_input())\n", "\n", "d = b*b - 4*a*c\n", "\n", "if d > 0:\n", " x1 = (-b + math.sqrt(d))/(2*a)\n", " x2 = (-b - math.sqrt(d))/(2*a)\n", " sys.stdout.write(\"\\nRoots are real and unequal\\n %6.2f %6.2f\"%(x1,x2))\n", "else:\n", " if d == 0:\n", " x = -b / (2*a)\n", " sys.stdout.write(\"\\nRoots are real and equal \\n %6.2f\"%(x))\n", " else:\n", " sys.stdout.write(\"\\nNo Real roots,roots are complex\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter coefficients a,b and c : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roots are real and unequal\n", " -1.00 -2.00" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8, Page Number : CSC-13" ] }, { "cell_type": "code", "collapsed": false, "input": [ "avg_marks = int(raw_input(\"\\nAverage Marks ? \"))\n", "\n", "if avg_marks >= 80 and avg_marks <= 100:\n", " print \"\\nHonours\"\n", "else:\n", " if avg_marks >= 60 and avg_marks <= 79:\n", " print \"\\nFirst Division\"\n", " else:\n", " if avg_marks >= 50 and avg_marks <= 59:\n", " print \"\\nSecond Division\"\n", " else:\n", " if avg_marks <= 49 and avg_marks >= 0:\n", " print \"\\nFail\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Average Marks ? 84\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Honours\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9, Page Number : CSC-14" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "units = int(raw_input(\"\\nEnter consumed units : \"))\n", "\n", "if units <= 200:\n", " amt = 0.5 * units\n", "else:\n", " if units <= 400:\n", " amt = 100 + 0.65 * (units - 200)\n", " else:\n", " if units <= 600:\n", " amt = 230 + 0.8 * (units - 400)\n", " else:\n", " amt = 425 + 1.25 * (units - 600)\n", " \n", "sys.stdout.write(\"\\nAmount to be paid Rs.%0.2f\"%(amt))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter consumed units : 348\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Amount to be paid Rs.196.20" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10, Page Number : CSC-16" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ts = int(raw_input(\"\\nEnter tensile strength : \"))\n", "rh = int(raw_input(\"\\nEnter rockwell hardness : \"))\n", "cc = int(raw_input(\"\\nEnter carbon content : \"))\n", "\n", "if ts >= 700:\n", " if rh >= 200:\n", " if cc <= 6:\n", " print \"\\nGrade is A\"\n", " else:\n", " print \"\\nGrade is B\"\n", " else:\n", " if cc <= 6:\n", " print \"\\nGrade is C\"\n", " else:\n", " print \"\\nGrade is E\"\n", "else:\n", " if rh >= 200:\n", " if cc <= 6:\n", " print \"\\nGrade is D\"\n", " else:\n", " print \"\\nGrade is E\"\n", " else:\n", " if cc <= 6:\n", " print \"\\nGrade is E\"\n", " else:\n", " print \"\\nGrade is F\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter tensile strength : 800\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter rockwell hardness : 180\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter carbon content : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Grade is C\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }