Chapter 5, Making Decisions in C¶

Program 5-2 , Page number: 65¶

In :
# Using If command

print "Enter a positive integer under 10: ",
n = 7
print n

if n == 7 :					# In this case it is 7
print "I knew you'd select 7!"
print "Thank you for your cooperation."
Enter a positive integer under 10:  7
I knew you'd select 7!

Program 5-3 , Page number: 67¶

In :
# Testing for leap years

year = 1948
print year
# If "year" is divisble by 4 and not divisible by 100 or divisible by 400, "year" is a leap year

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) :
print "%d is a leap year."%year
else :
print "%d is not a leap year."%year
1948 is a leap year.

Program 5-4 , Page number: 69¶

In :
# Illustration of if...else clause
# Program to tell if a number is odd or even

n = 13
print n

# A number is even if it is divisible by 2

if n % 2 != 0:
print "%d is odd"%n
else:
print "%d is even"%n
13 is odd

Program 5-5 , Page number: 70¶

In :
# Testing if a number is divisible by 7

print "Enter an integer:",
n = 13
print n

# A number is even if it is divisible by 2

if n % 7 == 0:
print "%d is divisible by 7"%n
else:
print "%d is not divisible by 7"%n
Enter an integer: 13
13 is not divisible by 7

Program 5-6 , Page number: 71¶

In :
# Determining which of 2 numbers is larger

print "Enter an integer:",
a = 4
print a
print "Enter another integer: ",
b = 7
print b

if a > b :
print "%d is greater than %d."%(a,b)
else:
print "%d is smaller than %d."%(a,b)
Enter an integer: 4
Enter another integer:  7
4 is smaller than 7.