Chapter 12 : The C PreProcessor

Example 12.1, Page No 272

In [1]:
print "\nHaving fun with preprocessor directives\n"
Having fun with preprocessor directives

Example 12.2, Page No 273

In [2]:
NUMBER = 7
print "\nLucky Number ", NUMBER , "\n"
Lucky Number  7 

Example 12.3, Page No 274

In [3]:
NUMBER = 7
print "\nLucky Number ", NUMBER , "\n"
NUMBER = 5
Lucky Number  7 

Example 12.4, Page No 275

In [4]:
def area(l,w):
    return l * w
length = 0
width = 0
length = int(raw_input("\nEnter length: "))
width = int(raw_input("\nEnter width: "))
print "\nArea of rectangle = " , area(length,width) , "\n"
Enter length: 12

Enter width: 7

Area of rectangle =  84 

Example 12.5, Page No 276

In [5]:
def RESULT(x,y):
    print "\nResult is \n", x+y
num1 = 0
num2 = 0
num1 = int(raw_input("\nEnter first number: "))
num2 = int(raw_input("\nEnter second number: "))
RESULT(num1,num2)
Enter first number: 27

Enter second number: 44

Result is 
71

Example 12.6, Page No 277

In [6]:
def RESULT(x,y):
    print "\nResult is \n", x+y
operand1 = 0
operand2 = 0
operand1 = int(raw_input("\nEnter first operand: "))
operand2 = int(raw_input("\nEnter second operand: "))
RESULT(operand1,operand2)
Enter first operand: 27

Enter second operand: 44

Result is 
71

Example 12.7, Page No 279

In [22]:
import profit
print "\nThe Profit Program\n"
price = float(raw_input("\nEnter unit price: "))
quantity = int(raw_input("Enter quantity sold: "))
totalCost = float(raw_input("Enter total cost: "))
profit1(price,quantity,totalCost)

"""
Header file:- profit.py
def profit(p,q,tc):
    print "\nYour profit is ",(p * q) - tc,"\n"
"""
The Profit Program


Enter unit price: 12.99
Enter quantity sold: 7
Enter total cost: 56

Your profit is  34.93 

Example 12.8, Page No 282

In [5]:
import calculate
selection = 0
print "\n The Function Wizard \n"
print "\n1\tDetermine perimeter of ractangle\n"
print "\n2\tDetermine area of rectangle\n"
print "\n3\tDetermine volume of rectangle\n"
selection = int(raw_input("\nEnter selection : "))
if selection == 1:
    l = float(raw_input("Enter length: "))
    w = float(raw_input("Enter width: "))
    perimeter(l,w)
elif selection == 2:
    l = float(raw_input("Enter length: "))
    w = float(raw_input("Enter width: "))
    area(l,w)
elif selection == 3:
    l = float(raw_input("Enter length: "))
    w = float(raw_input("Enter width: "))
    h = float(raw_input("Enter height: "))
    volume(l,w,h)
 The Function Wizard 


1	Determine perimeter of ractangle


2	Determine area of rectangle


3	Determine volume of rectangle


Enter selection : 3
Enter length: 1
Enter width: 2
Enter height: 3

 Perimeter is  6.0