{ "metadata": { "name": "", "signature": "sha256:c9e723d7feeae217ee8ddaf1b9cf8cfab2d696821fa911ff65107b9da6bec9f0" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4: Control Statements in C " ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1 , Page number: CP-60" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the biggest of two numbers\n", "# Variable declaration\n", "\n", "a = 5\n", "b = 8\n", "\n", "print \"Enter two numbers : %d\" % a,b\n", "\n", "# Calculation\n", "\n", "big = a\n", "if (b>big):\n", " big = b\n", "\n", "print \"Biggest number is \",big" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two numbers : 5 8\n", "Biggest number is 8\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2 , Page number: CP-61" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find biggest of three numbers\n", "# Variable declaration\n", "\n", "a = 5\n", "b = 13\n", "c = 8\n", "\n", "print \"Enter three numbers : %d\" % a,b,c\n", "\n", "# Calculate\n", "\n", "big = a\n", "if (b>big):\n", " big = b\n", "if (c>big):\n", " big = c\n", "\n", "print \"Biggest number is\",big" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three numbers : 5 13 8\n", "Biggest number is 13\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3 , Page number: CP-63" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find biggest of three numbers\n", "# Variable declaration\n", "\n", "a = 18\n", "b = -5\n", "c = 13\n", " \n", "print \"Enter three numbers : %d\" % a,b,c\n", "\n", "# Calculation to find biggest number\n", "\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", "print \"Biggest number is\",big" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three numbers : 18 -5 13\n", "Biggest number is 18\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4 , Page number: CP-64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the value of y \n", "# Variable declration\n", "\n", "x = 0.42\n", "n = 5\n", "\n", "print \"Enter value to x and n :\" , x,n \n", "\n", "# Calculation\n", "\n", "if (n==1):\n", " y = 1 + x\n", "elif (n==2):\n", " y = 1 + x / n\n", "elif (n==3):\n", " y = 1 + (x ** n)\n", "else:\n", " y = 1 + n * x\n", "\n", "\n", "print \"Value of y(x,n) = %0.2f\" % y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter value to x and n : 0.42 5\n", "Value of y(x,n) = 3.10\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5 , Page number: CP-66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the value of y\n", "# Variable declaration\n", "\n", "x = 0.42\n", "n = 5\n", "\n", "\n", "print \"Enter value to x and n :\", x,n\n", "\n", "# Calculation\n", "\n", "# Switch case statements \n", "if n == 1: # case 1\n", " y = 1 + x\n", "elif n == 2: # case 2\n", " y = 1 + x / n\n", "elif n == 3: # case 3\n", " y = 1 + (x ** n)\n", "else: # default\n", " y = 1 + n * x\n", "\n", "print \"Value of y(x,n) = %0.2f\" % y " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter value to x and n : 0.42 5\n", "Value of y(x,n) = 3.10\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6 , Page number: CP-68" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to caculate the commission for sales representatives\n", "# Variable declaration\n", "\n", "sales = 4500\n", "\n", "print \"Sales amount ? :\" , sales\n", "\n", "# Calculation of commission\n", "\n", "if (sales <= 500):\n", " comm = 0.05 * sales\n", "elif (sales <= 2000):\n", " comm = 35 + 0.10 * (sales - 500)\n", "elif (sales <= 5000):\n", " comm = 185 + 0.12 * (sales - 2000)\n", "else:\n", " comm = 0.125 * sales\n", "\n", "\n", "print \"Commission Amount Rs.%0.2f\" % comm" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sales amount ? : 4500\n", "Commission Amount Rs.485.00\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7 , Page number: CP-69" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find roots of a quadratic equation\n", "# Variable declaration\n", "\n", "a = 1\n", "b = 3\n", "c = 2\n", "\n", "print \"Enter coefficients a, b, and c :\", a,b,c\n", "\n", "d = b * b - 4 * a * c\n", "\n", "# Calculation of roots\n", "\n", "if (d > 0):\n", " x1 = (-b + (d ** 0.5)) / (2 * a)\n", " x2 = (-b - (d ** 0.5)) / (2 * a)\n", " print \"Roots are real and unequal \"\n", " print x1,x2\n", " \n", "\n", "elif(d == 0):\n", " x = -b / (2 * a)\n", " print \"Roots are real and equal\"\n", " print \"%6.2f\" % x\n", "\n", "else:\n", " print \"No Real roots, roots are complex\"\n", "\n", "\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter coefficients a, b, and c : 1 3 2\n", "Roots are real and unequal \n", "-1.0 -2.0\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8 , Page number: CP-70" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to print grade\n", "# Variable declaration\n", "\n", "avg_marks = 84\n", "\n", "print \"Average marks ?\",avg_marks\n", "\n", "# Calculation of grade\n", "\n", "if (avg_marks >= 80) and (avg_marks <= 100):\n", " print \"Honours\"\n", "elif (avg_marks >= 60) and (avg_marks <=79):\n", " print \"First Division\"\n", "elif (avg_marks >= 50) and (avg_marks <= 59):\n", " print \"Second Division\"\n", "else:\n", " print \"Fail\"\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Average marks ? 84\n", "Honours\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9 , Page number: CP-70" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to calculate electirc charges for domestic consumers\n", "# Variable declaration\n", "\n", "units = 348\n", "\n", "print \"Enter consumed units :\",units\n", "\n", "# Calculation of electric charges\n", "\n", "if (units <= 200):\n", " amt = 0.5 * units\n", "elif (units <= 400):\n", " amt = 100 + 0.65 * (units - 200)\n", "elif (units <= 600):\n", " amt = 230 + 0.8 * (units - 400)\n", "else:\n", " amt = 425 + 1.25 * (units - 600)\n", "print \n", "print \"Amount to be paid Rs.%0.2f\" % amt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter consumed units : 348\n", "\n", "Amount to be paid Rs.196.20\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10 , Page number: CP-73" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Program to find the grade of steel samples\n", "# Variable declaration\n", "\n", "ts = 800\n", "rh = 180\n", "cc = 3\n", "\n", "print \"Enter tensile strength :\",ts\n", "print \"Enter rockwell hardness :\",rh\n", "print \"Enter carbon content :\",cc\n", "\n", "# Calculation of grade\n", "\n", "if (ts >= 700):\n", " if (rh >= 200):\n", " if (cc <= 6):\n", " print \"Grade is A\"\n", " else:\n", " print \"Grade is B\" \n", " elif (cc <= 6):\n", " print \"Grade is C\"\n", " else:\n", " print \"Grade is E\" \n", "elif (rh >= 200):\n", " if (cc <= 6):\n", " print \"Grade is D\"\n", " else:\n", " print \"Grade is E\"\n", "elif (cc <= 6):\n", " print \"Grade is E\"\n", "else:\n", " print \"Grade is F\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter tensile strength : 800\n", "Enter rockwell hardness : 180\n", "Enter carbon content : 3\n", "Grade is C\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }