#Factorial of an integer number.
def rec(x):
f=0
if x==1:
return (1)
else:
f=x*rec(x-1)
return(f)
a=input("Enter the number:")
print "The Factorial value of %d:"%a,rec(a)
#Program to read characters and print reversely using recursion.
def reverse(c):
if len(c)<=0:
return c
return reverse(c[1:])+c[0]
c=raw_input("Enter the Line of Text...")
print reverse(c)
#concept of recursion
def add(pk, pm):
if pm==0:
return pk
else:
return(1+add(pk,pm-1))
k=70
m=20
i=add(k,m)
print "The Value of addition is %d"%i
#Tower of honoi
n=int(raw_input("How many Disks ...\n"))
def honoi(n,fro,to,t):
if(n>0):
honoi(n-1,fro,t,to)
print "Move disk %d from %c to %c\n"%(n,fro,to)
honoi(n-1,t,to,fro)
fro='L'
to='R'
t='C'
honoi(n,fro,to,t)
#user defined function that computes 'x' raised to the power 'y'
import math
x,y=input("Enter the Values of X and Y ")
print "%d to the power of %d is ... "%(x,y),math.pow(x,y)
#nesting of function
def ratio(x,y,z):
def diff(m,n):
if(m!=n):
return(1)
else:
return(0)
diff(y,z)
if diff(y,z):
return(x/(y-z))
else:
return(0)
x,y,z=input("Enter the values of X,Y,Z: ")
print "Ratio is..%f\n"%ratio(x,y,z)
#Demonstration of Global Variable
a=39 #Global Variable
print "A... %d\n"%a
def f1():
global a
a=a+10
return a
def f2():
global a
a=a+20
return a
def f3():
global a
a=a+30
return a
print "A...%d\n"%f1()
print "A...%d\n"%f2()
print "A...%d\n"%f3()