# Function declaration and result
a = 5
c = 'h'
b = 3.14
def myfunction (x, ch, fl) :
print ('%d %c %.2f ' % (x, ch, fl))
myfunction (a, c, b)
# 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) :
som = a + b
return som
print ('The sum of %d and %d is %d' % (a, b, (add (a, b))))
# 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 :
return a
else :
return b
print ('The greater number is %d' % (greater (a, b)))
# 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 :
print ('Both numbers are equal')
elif a > b :
print ('The greater number is %d' % a)
else :
print ('The greater number is %d' % b)
return
greater (a, b)
# 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 greatest (a, b, c) :
if a > b :
if a > c :
return a
else :
return c
else :
if b > c :
return b
else :
return c
print ('The greatest number is %d' % (greatest (a, b, c)))
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def printnum (n) :
print ('Counting upto %d' % n)
for i in range (1, n+1) :
print i,
printnum (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def printeven (n) :
print ('Even numbers upto %d' % n)
for i in range (2, n+1, 2) :
print i,
printeven (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def printodd (n) :
print ('Odd numbers upto %d' % n)
for i in range (1, n+1, 2) :
print i,
printodd (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def printnum (n) :
print ('Number line for %d' % n)
for i in range (-n, n+1) :
print i,
printnum (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def summate (n) :
total = 0
for i in range (1, n+1) :
total = total + i
return total
print('Sum of first %d natural numbers is %d' % (n, summate (n)))
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def factors (n) :
print ('The factors of %d are' % n)
for i in range (1, n+1) :
if n%i == 0 :
print i,
factors (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def multiple (n) :
print ('The first 10 multiples of %d are' % n)
for i in range (1, 11) :
print('%d X %d = %d' % (n, i, n*i))
multiple (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def sumofdigit (n) :
sum = 0
while n>0 :
sum = sum + n%10
n = n/10
return sum
print ('The sum of digits of %d is %d' % (n, sumofdigit (n)))
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def prime (n) :
count = 0
for i in range (2, n) :
if n%i == 0 :
count = count + 1
break
if count == 0 :
print ('The number %d is prime' % n)
else :
print ('The number %d is composite' % n)
prime (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
print ('%d terms of Fibonacci series are ' % n)
def printfibo (n) :
first = 0
second = 1
count = 3
print ('%d %d ' % (first, second)),
while count<=n :
third = first + second
first = second
second = third
print (' %d ' % third),
count = count + 1
printfibo (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def sumofdigit (n) :
sum = n
while sum>=10 :
sum = 0
while n>0 :
sum = sum + n%10
n = n/10
n = sum
return sum
print ('Sum of digits of %d is %d' % (n, sumofdigit (n)))
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
print ('Prime numbers upto %d are ' % n)
def printprime (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),
printprime (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def pattern (n) :
for lin in range (1, n+1) :
for count in range (1, lin+1) :
print ('%d ' % count),
print
pattern (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def pattern (n) :
for lin in range (1, n+1) :
for count in range (lin, 2*lin) :
print ('%d ' % count),
print
pattern (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def pattern (n) :
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
pattern (n)
# Variable declaration
n = int(raw_input('Enter a number: '))
# Function declaration, calculation and result
def pattern (n) :
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
pattern (n)