Chapter 3: Simple Programs

Example 1, Page number: 67

In [14]:
# Calculation and result
pi = 3.141
num = int(raw_input('Enter your favorite number: '))
print ('You have entered your favorite number as %d' % num)
c = raw_input('Enter your favorite character: ')
print ('You have entered your favorite character as %c' % c)
print ('The value of pi is %.3f ' % pi)
Enter your favorite number: 3
You have entered your favorite number as 3
Enter your favorite character: m
You have entered your favorite character as m
The value of pi is 3.141 

Example 3.1, Page number: 70

In [2]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Calculation
som = a+b

# Result
print ('The sum of %d + %d = %d ' % (a, b, som))
Enter first number 67
Enter second number 990
The sum of 67 + 990 = 1057 

Example 3.2, Page number: 71

In [3]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Calculation and result
if a>b :
   print ('The greater number is %d ' % a)
else :
   print ('The greater number is %d ' % b)
Enter first number 78
Enter second number 190
The greater number is 190 

Example 3.3, Page number: 72

In [5]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))

# Calculation and result
if a==b :
   print ('Both numbers are equal ')
else :
   if a>b :
      print ('The greater number is %d ' % a)
   else :
      print ('The greater number is %d ' % b)
Enter first number 78
Enter second number 78
Both numbers are equal 

Example 3.4, Page number: 73

In [1]:
# Variable declaration
a = int(raw_input('Enter first number '))
b = int(raw_input('Enter second number '))
c = int(raw_input('Enter third number '))

# Calculation and result
if a>b :
   if a>c :
      print ('The greatest number is %d ' % a)
   else :
      print ('The greatest number is %d ' % c)

else :
   if b>c :
      print ('The greatest number is %d ' % b)
   else : 
      print ('The greatest number is %d ' % c)
Enter first number 45
Enter second number 128
Enter third number 90
The greatest number is 128 

Example 3.5, Page number: 74

In [1]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('Counting upto %d ' % n)

for i in range (1, n+1) :
   print ('%d ' % i),
Enter any number 7
Counting upto 7 
1  2  3  4  5  6  7 

Example 3.6, Page number: 75

In [3]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('Even numbers upto %d ' % n)

for i in range (2, n+1, 2) :
   print ('%d ' % i),
Enter any number 10
Even numbers upto 10 
2  4  6  8  10 

Example 3.7, Page number: 75

In [13]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('Odd numbers upto %d ' % n)

for i in range (1, n+1, 2) :
   print ('%d ' % i),
Enter any number 10
Odd numbers upto 10 
1  3  5  7  9 

Example 3.8, Page number: 76

In [5]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('Number line upto %d ' % n)

for i in range (-n, n+1) :
   print ('%d ' % i),
Enter any number 7
Number line upto 7 
-7  -6  -5  -4  -3  -2  -1  0  1  2  3  4  5  6  7 

Example 3.9, Page number: 77

In [6]:
# Variable declaration
n = int(raw_input('Enter any number '))
sum = 0

# Calculation
for i in range (1, n+1) :
   sum = sum + i

# Result
print ('Sum of %d natural numbers is %d ' % (n, sum))
Enter any number 6
Sum of 6 natural numbers is 21 

Example 3.10, Page number: 78

In [7]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('The factors of %d are ' % n)

for i in range (1, n+1) :
   if n%i == 0 :
      print ('%d ' % i),
Enter any number 28
The factors of 28 are 
1  2  4  7  14  28 

Example 3.11, Page number: 79

In [8]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('The first 10 multiples of %d are ' % n)

for i in range (1, 11) :
   print ('%d X %d = %d ' % (n, i, n*i))
Enter any number 6
The first 10 multiples of 6 are 
6 X 1 = 6 
6 X 2 = 12 
6 X 3 = 18 
6 X 4 = 24 
6 X 5 = 30 
6 X 6 = 36 
6 X 7 = 42 
6 X 8 = 48 
6 X 9 = 54 
6 X 10 = 60 

Example 3.12, Page number: 80

In [12]:
# Variable declaration
n = int(raw_input('Enter any number '))
i = 1
sum = 0

# Calculation
print ('The sum of digits of %d is' % n),

while n > 0 :
   sum = sum + n%10
   n = n/10

# Result
print ('%d' % sum)
Enter any number 5214
The sum of digits of 5214 is 12

Example 3.13, Page number: 81

In [10]:
# Variable declaration
n = int(raw_input('Enter any number '))
flag = 1

# Calculation
for i in range (2, n) :
   if flag == 1 :
      if n%i == 0 :
         flag = 0

# Result
if flag :
   print ('%d is Prime ' % n)
else :
   print ('%d is Composite ' % n)
Enter any number 41
41 is Prime 

Example 3.14, Page number: 82

In [14]:
# Variable declaration
n = int(raw_input('Enter any number '))
first = 0
second = 1
count = 3

# Calculation and result
print ('%d Fibonacci terms are ' % n)
print ('%d  %d ' % (first, second)),

while count<=n :
   third = first + second
   first = second
   second = third
   print ('%d ' % third),
   count = count + 1
Enter any number 10
10 Fibonacci terms are 
0  1  1  2  3  5  8  13  21  34 

Example 3.15, Page number: 83

In [15]:
# Variable declaration
n = int(raw_input('Enter any number '))
sum = n

# Calculation
print ('Sum of digits of %d is' % n),

while sum>=10 :
   sum = 0
   while n>0 :
      sum = sum + n%10
      n = n/10
   n = sum

# Result
print ('%d' % sum)
Enter any number 8626
Sum of digits of 8626 is 4

Example 3.16, Page number: 84

In [16]:
# Variable declaration
n = int(raw_input('Enter any number '))

# Calculation and result
print ('Prime numbers upto %d are ' % n)

for num in range (2, n+1) :
   flag = 1
   for i in range (2, num) :
      if num % i == 0 : 
         flag = 0
   if flag :
      print ('%d ' % num),
Enter any number 12
Prime numbers upto 12 are 
2  3  5  7  11 

Example 3.17, Page number: 85

In [17]:
# Variable declaration
n = int(raw_input('Enter a number '))

# Calculation and result
for lin in range (1, n+1) :
   for count in range (1, lin+1) :
      print ('%d ' % count),
   print
Enter a number 6
1 
1  2 
1  2  3 
1  2  3  4 
1  2  3  4  5 
1  2  3  4  5  6 

Example 3.18, Page number: 86

In [18]:
# Variable declaration
n = int(raw_input('Enter a number '))

# Calculation and result
for lin in range (1, n+1) :
   for count in range (lin, 2*lin) :
      print ('%d ' % count),
   print
Enter a number 6
1 
2  3 
3  4  5 
4  5  6  7 
5  6  7  8  9 
6  7  8  9  10  11 

Example 3.19, Page number: 87

In [19]:
# Variable declaration
n = int(raw_input('Enter a number '))

# Calculation and result
for lin in range (1, n+1) :
   for blk in range (1, n-lin+1) :
      print ' ',
   for up in range (1, lin+1) :
      print ('%d' % up),
   down = lin-1
   while down>0 :
      print ('%d' % down),
      down = down-1
   print
Enter a number 6
          1
        1 2 1
      1 2 3 2 1
    1 2 3 4 3 2 1
  1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1

Example 3.20, Page number: 88

In [20]:
# Variable declaration
n = int(raw_input('Enter a number '))

# Calculation and result
for lin in range (1, n+1) :
   for blk in range (1, n-lin+1) :
      print ' ',
   for up in range (lin, 2*lin) :
      print ('%d' % (up%10)),
   down = 2*lin-2
   while down>=lin :
      print ('%d' % (down%10)),
      down = down-1
   print
Enter a number 6
          1
        2 3 2
      3 4 5 4 3
    4 5 6 7 6 5 4
  5 6 7 8 9 8 7 6 5
6 7 8 9 0 1 0 9 8 7 6