# Chapter 04: Control Statements in C¶

## Example 1 , Page number: CP-60¶

In :
# Program to find the biggest of two numbers
# Variable declaration

a = 5
b = 8

print "Enter two numbers : %d" % a,b

# Calculation

big = a
if (b>big):
big = b

print "Biggest number is ",big

Enter two numbers : 5 8
Biggest number is  8


## Example 2 , Page number: CP-61¶

In :
# Program to find biggest of three numbers
# Variable declaration

a = 5
b = 13
c = 8

print "Enter three numbers : %d" % a,b,c

# Calculate

big = a
if (b>big):
big = b
if (c>big):
big = c

print "Biggest number is",big

Enter three numbers : 5 13 8
Biggest number is 13


## Example 3 , Page number: CP-63¶

In :
# Program to find biggest of three numbers
# Variable declaration

a = 18
b = -5
c = 13

print "Enter three numbers : %d" % a,b,c

# Calculation to find biggest number

if (a>b):
if(a>c):
big = a
else:
big = c
else:
if(b>c):
big = b
else:
big = c

print "Biggest number is",big

Enter three numbers : 18 -5 13
Biggest number is 18


## Example 4 , Page number: CP-64¶

In :
# Program to find the value of y
# Variable declration

x = 0.42
n = 5

print "Enter value to x and n :" , x,n

# Calculation

if (n==1):
y = 1 + x
elif (n==2):
y = 1 + x / n
elif (n==3):
y = 1 + (x ** n)
else:
y = 1 + n * x

print "Value of y(x,n) = %0.2f" % y

Enter value to x and n : 0.42 5
Value of y(x,n) = 3.10


## Example 5 , Page number: CP-66¶

In :
# Program to find the value of y
# Variable declaration

x = 0.42
n = 5

print "Enter value to x and n :", x,n

# Calculation

# Switch case statements
if n == 1: # case 1
y = 1 + x
elif  n == 2: # case 2
y = 1 + x / n
elif n == 3: # case 3
y = 1 + (x ** n)
else: # default
y = 1 + n * x

print "Value of y(x,n) = %0.2f" % y

Enter value to x and n : 0.42 5
Value of y(x,n) = 3.10


## Example 6 , Page number: CP-68¶

In :
# Program to caculate the commission for sales representatives
# Variable declaration

sales = 4500

print "Sales amount ? :" , sales

# Calculation of commission

if (sales <= 500):
comm = 0.05 * sales
elif (sales <= 2000):
comm = 35 + 0.10 * (sales - 500)
elif (sales <= 5000):
comm = 185 + 0.12 * (sales - 2000)
else:
comm = 0.125 * sales

print "Commission Amount Rs.%0.2f" % comm

Sales amount ? : 4500
Commission Amount Rs.485.00


## Example 7 , Page number: CP-69¶

In :
# Program to find roots of a quadratic equation
# Variable declaration

a = 1
b = 3
c = 2

print "Enter coefficients a, b, and c :", a,b,c

d = b * b - 4 * a * c

# Calculation of roots

if (d > 0):
x1 = (-b + (d ** 0.5)) / (2 * a)
x2 = (-b - (d ** 0.5)) / (2 * a)
print "Roots are real and unequal "
print x1,x2

elif(d == 0):
x = -b / (2 * a)
print "Roots are real and equal"
print "%6.2f" % x

else:
print "No Real roots, roots are complex"


Enter coefficients a, b, and c : 1 3 2
Roots are real and unequal
-1.0 -2.0


## Example 8 , Page number: CP-70¶

In :
# Program to print grade
# Variable declaration

avg_marks = 84

print "Average marks ?",avg_marks

# Calculation of grade

if (avg_marks >= 80) and (avg_marks <= 100):
print "Honours"
elif (avg_marks >= 60) and (avg_marks <=79):
print "First Division"
elif (avg_marks >= 50) and (avg_marks <= 59):
print "Second Division"
else:
print "Fail"

Average marks ? 84
Honours


## Example 9 , Page number: CP-70¶

In :
# Program to calculate electirc charges for domestic consumers
# Variable declaration

units = 348

print "Enter consumed units :",units

# Calculation of electric charges

if (units <= 200):
amt = 0.5 * units
elif (units <= 400):
amt = 100 + 0.65 * (units - 200)
elif (units <= 600):
amt = 230 + 0.8 * (units - 400)
else:
amt = 425 + 1.25 * (units - 600)
print
print "Amount to be paid Rs.%0.2f" % amt

Enter consumed units : 348

Amount to be paid Rs.196.20


## Example 10 , Page number: CP-73¶

In :
# Program to find the grade of steel samples
# Variable declaration

ts = 800
rh = 180
cc = 3

print "Enter tensile strength :",ts
print "Enter rockwell hardness :",rh
print "Enter carbon content :",cc

# Calculation of grade

if (ts >= 700):
if (rh >= 200):
if (cc <= 6):
print "Grade is A"
else:
print "Grade is B"
elif (cc <= 6):
print "Grade is C"
else:
print "Grade is E"
elif (rh >= 200):
if (cc <= 6):
print "Grade is D"
else:
print "Grade is E"
elif (cc <= 6):
print "Grade is E"
else:
print "Grade is F"

Enter tensile strength : 800
Enter rockwell hardness : 180
Enter carbon content : 3
Grade is C