# Chapter 7: Recursion¶

## Example 7.1, Page number: 219¶

In :
# Variable declaration, calculation and result
str = raw_input('Enter a string: ')

print str

Enter a string: HELLO
HELLO


## Example 7.2, Page number: 221¶

In :
# Variable declaration, calculation and result
str = raw_input('Enter a string: ')

print str[::-1]

Enter a string: HELLO
OLLEH


## Example 7.3, Page number: 224¶

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

# Function declaration, calculation and result
def fact (n) :
if n>0 :
return n * fact (n-1)
else :
return 1

print ('Factorial of %d is %d' % (n, fact (n)))

Enter any number 5
Factorial of 5 is 120


## Example 7.4, Page number: 227¶

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

# Function declaration, calculation and result
def printnum (n) :
if n>0 :
printnum (n-1)
print n,

print ('Counting up to %d' % n)
printnum (n)

Enter any number 10
Counting up to 10
1 2 3 4 5 6 7 8 9 10


## Example 7.5, Page number: 229¶

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

# Function declaration, calculation and result
def printeven (n) :
if not hasattr(printeven, "x") :
printeven.x = 2

if printeven.x <= n :
print printeven.x,
printeven.x = printeven.x + 2
printeven (n)

print ('Even numbers up to %d are' % n)
printeven (n)

Enter any number 10
Even numbers up to 10 are
2 4 6 8 10


## Example 7.6, Page number: 231¶

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

# Function declaration, calculation and result
def printodd (x, n) :
if x <= n :
print x,
printodd (x+2, n)

print ('Odd numbers up to %d are' % n)
printodd (1, n)

Enter any number 10
Odd numbers up to 10 are
1 3 5 7 9


## Example 7.7, Page number: 232¶

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

# Function declaration, calculation and result
def printnum (fro, to) :
if fro <= to :
print fro,
printnum (fro+1, to)

print ('Number line for %d is' % n)
printnum (-n, n)

Enter any number 5
Number line for 5 is
-5 -4 -3 -2 -1 0 1 2 3 4 5


## Example 7.8, Page number: 234¶

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

# Function declaration, calculation and result
def summate (n) :
if n > 0 :
return n + summate (n-1)
else :
return 0

print ('Sum of first %d natural numbers is %d' % (n, summate (n)))

Enter any number 10
Sum of first 10 natural numbers is 55


## Example 7.9, Page number: 237¶

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

# Function declaration, calculation and result
def factors (n) :
if not hasattr(factors, "i") :
factors.i = 1

if factors.i <= n :
if n % factors.i == 0 :
print factors.i,
factors.i = factors.i + 1
factors (n)

print ('The factors of %d are' % n)
factors (n)

Enter any number 28
The factors of 28 are
1 2 4 7 14 28


## Example 7.10, Page number: 239¶

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

# Function declaration, calculation and result
def multiple (n) :
if not hasattr(multiple, "i") :
multiple.i = 1

if multiple.i <= 10 :
print ('%d X %d = %d' % (n, multiple.i, n*multiple.i))
multiple.i = multiple.i + 1
multiple (n)

print ('The first 10 multiples of %d are' % n)
multiple (n)

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 7.11, Page number: 241¶

In :
# 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 ('The number %d is PRIME ' % n)
else :
print ('The number %d is COMPOSITE ' % n)

Enter any number 19
The number 19 is PRIME


## Example 7.12, Page number: 243¶

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

# Function declaration, calculation and result
def Fibonacci (n) :
if n < 2 :
return n
else :
return Fibonacci (n-1) + Fibonacci (n-2)

print ('The %d terms of Fibonacci series are' % n)
for i in range (0, n) :
print ('%d' % Fibonacci (i)),

Enter any number 10
The 10 terms of Fibonacci series are
0 1 1 2 3 5 8 13 21 34


## Example 7.13, Page number: 244¶

In :
# 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 8626
Sum of digits of 8626 is 4


## Example 7.14, Page number: 245¶

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

# Function declaration, calculation and result
def prime (n) :
for num in range (3, n+1) :
flag = 1
for i in range (2, num) :
if num % i == 0 :
flag = 0
if flag :
print ('%d ' % num),

print ('Prime numbers upto %d are' % n)
prime (n)

Enter any number 30
Prime numbers upto 30 are
3  5  7  11  13  17  19  23  29


## Example 7.15, Page number: 246¶

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

# Function declaration, calculation and result
def pattern (n) :
if n > 0 :
pattern (n-1)
for count in range (1, n+1) :
print ('%d' % count),
print

pattern (n)

Enter any number 4

1
1 2
1 2 3
1 2 3 4


## Example 7.16, Page number: 249¶

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

# Function declaration, calculation and result
def printline (x, total) :
if x > 0 :
printline (x-1, total)
for blk in range (1, total-x+1) :
print ' ',
for up in range (1, x+1) :
print ('%d' % up),
down = x-1
while down>0 :
print ('%d' % down),
down = down-1
print

printline (n, n)

Enter any number 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1


## Example 7.17, Page number: 253¶

In :
# Function declaration, calculation and result
def listsum () :
n = int(raw_input('Enter any number '))
if n > 0 :
return n + listsum ()
else :
return 0

sum = listsum ()
print ('Sum of list of numbers is %d' % sum)

Enter any number 1
Enter any number 2
Enter any number 3
Enter any number -4
Sum of list of numbers is 6