Chapter 5: Pointers

Example 5.1, Page number: 135

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

# Calculation and result
print ('The original values are a = %d b = %d' % (a, b))
temp = a
a = b
b = temp

print ('The values after swapping are a = %d b = %d' % (a, b))
Enter first number: 15
Enter second number: 27
The original values are a = 15 b = 27
The values after swapping are a = 27 b = 15

Example 5.2, Page number: 136

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

# Function declaration, calculation and result
print ('The original values are a = %d b = %d' % (a, b))

def swap (a, b) :
   temp = a
   a = b
   b = temp
   print ('The values after swapping are a = %d b = %d' % (a, b))

swap (a, b)
Enter first number: 15
Enter second number: 27
The original values are a = 15 b = 27
The values after swapping are a = 27 b = 15

Example 5.3, Page number: 141

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

# Function declaration, calculation and result
print ('The original values are a = %d b = %d' % (a, b))

def swap (a, b) :
   temp = a
   a = b
   b = temp
   print ('The values after swapping are a = %d b = %d' % (a, b))

swap (a, b)
Enter first number: 15
Enter second number: 27
The original values are a = 15 b = 27
The values after swapping are a = 27 b = 15

Example 5.4, Page number: 145

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

# Function declaration, calculation and result
print ('In main the original values are a = %d b = %d' % (a, b))
print ('In main the address of a is %x and address of b is %x' % (a, b))

def swap (a, b) :
   print ('In swap the address in a is %x and b is %x' % (a, b))
   temp = a
   a = b
   b = temp
   print ('The values after swapping are a = %d b = %d' % (a, b))

swap (a, b)

print ('In main the values after swapping are a = %d b = %d' % (a, b))
Enter first number: 15
Enter second number: 27
In main the original values are a = 15 b = 27
In main the address of a is f and address of b is 1b
In swap the address in a is f and b is 1b
The values after swapping are a = 27 b = 15
In main the values after swapping are a = 15 b = 27

Example 5.5, Page number: 148

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

# Function declaration, calculation and result
print ('In main the value of a = %d b = %d' % (a, b))

def doooo (a, b) :
   a = a+10
   b = b+10

doooo (a, b)

print ('In main the value of a = %d b = %d' % (a, b))
Enter first number: 5
Enter second number: 7
In main the value of a = 5 b = 7
In main the value of a = 5 b = 7

Example 5.6, Page number: 150

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

# Function declaration, calculation and result
print ('In main the value of a = %d b = %d' % (a, b))
print ('In main the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))

def doooo (a, b) :
   print ('In doooo the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))
   a = a+10
   b = b+10

doooo (a, b)

print ('In main the value of a = %d b = %d' % (a, b))
Enter first number: 5
Enter second number: 7
In main the value of a = 5 b = 7
In main the addresses are a = 0x1b49138 b = 0x1b49108
In doooo the addresses are a = 0x1b49138 b = 0x1b49108
In main the value of a = 5 b = 7

Example 5.7, Page number: 152

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

# Function declaration, calculation and result
def add (a, b) :
   x = a+b
   return x

som = add (a, b)

print ('The sum of %d + %d = %d' % (a, b, som))
Enter first number 2
Enter second number 3
The sum of 2 + 3 = 5

Example 5.8, Page number: 154

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

# Function declaration, calculation and result
def greater (a, b) :
   if a>b :
      big = a
   else :
      big = b
   return big

great = greater (a, b)

print ('The greater number is %d' % great)
Enter first number 45
Enter second number 23
The greater number is 45

Example 5.9, Page number: 156

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

# Function declaration, calculation and result
def greater (a, b, c) :
   if a>b :
      if a>c :
         big = a
      else :
         big = c
   else :
      if b>c :
         big = b
      else :
         big = c
   return big

great = greater (a, b, c)

print ('The greatest number is %d' % great)
Enter first number 45
Enter second number 190
Enter third number 128
The greatest number is 190

Example 5.10, Page number: 158

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

# Function declaration, calculation and result
def summate (n) :
   i = 1
   total = 0
   while i <= n :
      total = total + i
      i = i+1
   return total

sumn = summate (n)

print ('Sum of first %d natural numbers is %d' % (n, sumn))
Enter any number 5
Sum of first 5 natural numbers is 15

Example 5.11, Page number: 160

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

# Function declaration, calculation and result
def sumofdigit (n) :
   sum = 0
   while n > 0 :
      sum = sum + n%10
      n = n/10
   return sum

result = sumofdigit (n)

print ('Sum of digits of %d is %d' % (n, result))
Enter any number 7162
Sum of digits of 7162 is 16

Example 5.12, Page number: 161

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

# Function declaration, calculation and result
def sumofdigit (n, sum) :
   while sum>=10 :
      sum = 0
      while n>0 :
         sum = sum + n%10
         n = n/10
      n = sum
   return sum

result = sumofdigit (n, sum)

print ('Sum of digits of %d is %d' % (n, result))
Enter any number 7162
Sum of digits of 7162 is 7

Example 5.13, Page number: 163

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

# Function declaration, calculation and result
def checkprime (n) :
   flag = 1
   i = 2
   while i<n and flag :
      if n%i == 0 :
         flag = 0
      i = i+1
   return flag

result = checkprime (n)

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

Example 5.14, Page number: 165

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

# Function declaration, calculation and result
def checkprime (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),

checkprime (n)
Enter any number 11
2  3  5  7  11 

Example 5.15, Page number: 167

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

# Function declaration, calculation and result
def makethird (n) :
   first = 0
   second = 1
   count = 3
   print ('%d terms of Fibonacci series are ' % n)
   print ('%d  %d ' % (first, second)),
   while count<=n :
      third = first + second
      first = second
      second = third
      print (' %d ' % third),
      count = count + 1

makethird (n)
Enter any number 10
10 terms of Fibonacci series are 
0  1   1   2   3   5   8   13   21   34 

Example 5.16, Page number: 169

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

# Function declaration, calculation and result
def makethird (n) :
   first = 0
   second = 1
   third = 0
   print ('Fibonacci series up to %d is ' % n)
   print first,
   while third<=n :
      print (' %d ' % second),
      third = first + second
      first = second
      second = third

makethird (n)
Enter any number 100
Fibonacci series up to 100 is 
0  1   1   2   3   5   8   13   21   34   55   89