# Variable declaration, calculation and result
str = raw_input('Enter a string: ')
print str
# Variable declaration, calculation and result
str = raw_input('Enter a string: ')
print str[::-1]
# 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)))
# 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)
# 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)
# 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)
# 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)
# 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)))
# 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)
# 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)
# 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)
# 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)),
# 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))
# 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)
# 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)
# 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)
# 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)