Chapter 5: Control Statements in C

Example 1, Page Number : CSC-3

In [1]:
import sys

#Variable Initialization
a = int(raw_input("\nEnter two numbers : "))
b = int(raw_input(""))
big = a

#Processing
if b > big:
    big = b

#Output
print "\nBiggest number is ",big
Enter two numbers : 5
8

Biggest number is  8

Example 2, Page Number : CSC-4

In [2]:
import sys

#Variable Initialization
a = int(raw_input("\nEnter three numbers : "))
b = int(raw_input(""))
c = int(raw_input(""))
big = a

#Processing
if b > big:
    big = b
    
if c > big:
    big = c
    
#Output
print "\nBiggest number is ",big
Enter three numbers : 5
13
8

Biggest number is  13

Example 3, Page Number : CSC-6

In [3]:
import sys

#Variable Initialization
a = int(raw_input("\nEnter three numbers : "))
b = int(raw_input(""))
c = int(raw_input(""))

#Processing
if a > b:
    if a > c:
        big = a
    else:
        big = c
else:
    if b > c:
        big = b
    else:
        big = c
        
#Output
print "\nBiggest number is ",big
Enter three numbers : 18
-5
13

Biggest number is  18

Example 4, Page Number : CSC-7

In [6]:
import sys

x = float(raw_input("\nEnter value to x and n : "))
n = int(raw_input(""))

if n == 1:
    y = 1 + x
else:
    if n == 2:
        y = 1 + x/n
    else:
        if n == 3:
            y = 1 + pow(x,n)
        else:
            y = 1 + n*x
            
print "\nValue of y(x,n) = ",y
Enter value to x and n : 0.42
5

Value of y(x,n) =  3.1

Example 5, Page Number : CSC-9

In [7]:
import sys

x = float(raw_input("\nEnter value to x and n : "))
n = int(raw_input(""))

#There is no switch statement in python
if n == 1:
    y = 1 + x
else:
    if n == 2:
        y = 1 + x/n
    else:
        if n == 3:
            y = 1 + pow(x,n)
        else:
            y = 1 + n*x
            
sys.stdout.write("\nValue of y(x,n) = %6.2f"%(y))
Enter value to x and n : 0.42
5

Value of y(x,n) =   3.10

Example 6, Page Number : CSC-11

In [8]:
import sys

sales = int(raw_input("\nSales amount ? "))

if sales <= 500:
    comm = 0.05 * sales
else:
    if sales <= 2000:
        comm = 35 + 0.10 * (sales - 500)
    else:
        if sales <= 5000:
            comm = 185 + 0.12 * (sales - 2000)
        else:
            comm = 0.125 * sales
            
sys.stdout.write("\nCommission Amount Rs. %0.2f"%(comm))
Sales amount ? 4500

Commission Amount Rs. 485.00

Example 7, Page Number : CSC-12

In [10]:
import sys
import math

a = int(raw_input("\nEnter coefficients a,b and c : "))
b = int(raw_input())
c = int(raw_input())

d = b*b - 4*a*c

if d > 0:
    x1 = (-b + math.sqrt(d))/(2*a)
    x2 = (-b - math.sqrt(d))/(2*a)
    sys.stdout.write("\nRoots are real and unequal\n %6.2f   %6.2f"%(x1,x2))
else:
    if d == 0:
        x = -b / (2*a)
        sys.stdout.write("\nRoots are real and equal \n  %6.2f"%(x))
    else:
        sys.stdout.write("\nNo Real roots,roots are complex")
Enter coefficients a,b and c : 1
3
2

Roots are real and unequal
  -1.00    -2.00

Example 8, Page Number : CSC-13

In [11]:
avg_marks = int(raw_input("\nAverage Marks ? "))

if avg_marks >= 80 and avg_marks <= 100:
    print "\nHonours"
else:
    if avg_marks >= 60 and avg_marks <= 79:
        print "\nFirst Division"
    else:
        if avg_marks >= 50 and avg_marks <= 59:
            print "\nSecond Division"
        else:
            if avg_marks <= 49 and avg_marks >= 0:
                print "\nFail"
Average Marks ? 84

Honours

Example 9, Page Number : CSC-14

In [12]:
import sys

units = int(raw_input("\nEnter consumed units : "))

if units <= 200:
    amt = 0.5 * units
else:
    if units <= 400:
        amt = 100 + 0.65 * (units - 200)
    else:
        if units <= 600:
            amt = 230 + 0.8 * (units - 400)
        else:
            amt = 425 + 1.25 * (units - 600)
            
sys.stdout.write("\nAmount to be paid Rs.%0.2f"%(amt))
Enter consumed units : 348

Amount to be paid Rs.196.20

Example 10, Page Number : CSC-16

In [13]:
ts = int(raw_input("\nEnter tensile strength : "))
rh = int(raw_input("\nEnter rockwell hardness : "))
cc = int(raw_input("\nEnter carbon content : "))

if ts >= 700:
    if rh >= 200:
        if cc <= 6:
            print "\nGrade is A"
        else:
            print "\nGrade is B"
    else:
        if cc <= 6:
            print "\nGrade is C"
        else:
            print "\nGrade is E"
else:
    if rh >= 200:
        if cc <= 6:
            print "\nGrade is D"
        else:
            print "\nGrade is E"
    else:
        if cc <= 6:
            print "\nGrade is E"
        else:
            print "\nGrade is F"
Enter tensile strength : 800

Enter rockwell hardness : 180

Enter carbon content : 3

Grade is C
In [ ]: