# Program to write a function to find factorial and use it to find nCr
# Variable declaration
n = 5
r = 3
print "Enter value to n and r :",n,r
# function subprogram to find factorial
def fact(k):
p = 1
for i in range(1,k+1):
p = p * i
return p
ncr = fact(n) / (fact(r) * fact(n-r))
print "Value of nCr =",ncr
# Program to find the biggest of given three values using function and use it to find total marks of the student
# Variable declaration
t1 = 62
t2 = 70
t3 = 58
a1 = 17
a2 = 21
a3 = 23
print "Enter three test scores :",t1,t2,t3
print "Enter three assignment scores :",a1,a2,a3
# function to find biggest of three values
def big(a,b,c):
if a > b:
if a > c:
return a
else:
return c
elif b > c:
return b
else:
return c
total = big(t1,t2,t3) + big(a1,a2,a3)
print "Total marks =",total
# Program to create a function to compute the cos(x) series upto 15 terms
# Variable declaration
x = 0
print "----------------------------------"
print " x in degrees cos(x) "
print "----------------------------------"
# function for computing cosine function
def cosine(x):
s = 0
i = 0
term = 1
x = x * (3.14) / 180
k = 1
# loop to find summation of 15 terms
while k <= 15:
s = s + term
term = term * x * x * (-1) / ((i + 1) * (i + 2))
i = i + 2
k = k + 1
return s
# calling function
while x <= 180:
print " %d " % x," %0.2f "%cosine(x)
x = x + 30
print "-----------------------------------"
# Program to find GCD of two integers
# Variable declaration
a = 45
b = 27
c = 81
print "Enter three integers :",a,b,c
def gcd(x,y):
if x >= y:
nr = x
dr = y
else:
nr = y
dr = x
r = nr % dr
while r != 0:
nr = dr
dr = r
r = nr % dr
return dr
d1 = gcd(a,b)
d2 = gcd(a,c)
d3 = gcd(b,c)
if d1 == d2:
if d1 == d3:
print "Greatest common divisor is",d1
else:
print "Greatest common divisor is ",gcd(d1,d3)
else:
print "Greatest common divisr is",gcd(d1,d2)
print "Press any key to continue. . ."
# Program to reverse the given integer
# Variable declaration
n = 2846
print "Enter the integer :",n
# function to reverse an integer
def reverse(n):
rn = 0
while n > 0:
r = n % 10
rn = rn * 10 + r
n = n /10
return rn
# call the function to print the reverse integer
print n,"is reversed as ",reverse(n)
print "Press any key to continue. . ."
# Program to compare two strings S1 and S2 and return the result 0,1,-1
# Variable declaration
s1 = "MUMBAI"
s2 = "MYSORE"
print "Enter the first string :",s1
print "Enter the second string :",s2
# function to compare the strings
def compare(s1,s2):
if cmp(s1,s2) == 0:
return 0
if cmp(s1,s2) > 0:
return 1
if cmp(s1,s2) < 0:
return -1
# call the function to print the result
print "The result is ", compare(s1,s2)
# Program to find the arithmetic mean of n values using a function
# Variable declaration
n = 6
x = [3.1,3.8,3.6,4.0,3.4,3.8]
print "How many values ?",n
print "Enter all values"
for i in x:
print i,
print
# function to find arithmetic mean
def amean(x,n):
s = 0
for i in range(n):
s = s + x[i]
return (s/n)
# call function to print arithmetic mean
print "Arithmetic mean = %0.2f" % amean(x,n)
# Program to read a matrix of order m x n and print the sum of all elements using functions
# Variable declaration
m = 3
n = 2
a = [[2,3,4],
[2,0,1]]
print "How many rows and columns :",m,n
print "Enter the matrix values"
for i in range(n):
for j in range(m):
print a[i][j],
print
# function to add all elements in the matrix
def elem_sum(a,m,n):
i = 0
j = 0
s = 0
for i in range(n):
for j in range(m):
s = s + a[i][j]
j = j + 1
i = i + 1
return s
# call function to print the result
print "Sum of all elements in the matrix =",
print elem_sum(a,m,n)
# Program to reverse a string
# Variable declaration
st = "NEW DELHI"
print "Enter a string :",st
def reverse(st):
rst = ""
for i in range(0 ,len(st)):
rst += st[(len(st) -1) - i]
return rst
print st,"is reversed as ",
print reverse(st)
# Program to apply binary search to set of N numbers using a function
# Variable declaration
n = 10
x = [-3,8,13,19,21,25,26,29,35,42]
s = 19
print "How many numbers ?",n
print "Enter all numbers in the list"
for a in x:
print a,
print
print "Enter the number to be searched :",s
# function to search the number
def bi_search(x,n,s):
flag = 0
start = 0
end = n
while start < end and flag ==0:
mid = (start + end) / 2
if x[mid] > s:
end = mid
elif x[mid] < s:
start = mid + 1
else:
flag = 1
return flag
# calling the function
if bi_search(x,n,s):
print "The number",s,"is present in the list"
else:
print "The number",s,"is not present in list"
# Program to find factorial of a given number using recursive function
# Variable declaration
n = 5
r = 3
print "Enter the values to n and r :",n,r
# recursive function to find factorial
def fact(k):
if k ==1:
return 1
else:
return (k * fact(k-1))
ncr = fact(n)/(fact(r) * fact(n-r))
print "Value of nCr =",ncr
# Program to compute the value of x power n using a recursive function
# Variable declaration
x = 4.20
n = 3
print "Enter value to x :",x
print "Enter its power :",n
# recursive function to find x rise to n
def power(x,n):
if n == 1:
return x
else:
return (x * power(x,n-1))
print "%0.2f" %x,"raise to ",n,"is %0.2f" % power(x,n)
# Program to display first n terms of the fibonacci series using recursive function
# Variable declaration
n = 10
t1 = 0
t2 = 1
i = 0
count = 2
# recursive function to print the terms in series
def fibo(t1,t2):
global count
if (count >= n):
return
else:
t3 = t1 + t2
print t3,
count = count + 1
t1 = t2
t2 = t3
return fibo(t1,t2)
print "How many terms to be printed ?",n
print "The first",n,"terms in Fibonacci series are"
print t1,
print t2,
fibo(t1,t2)
# Program to add two matrices of order m x n and to print the resultant values
# Variable declaration
m = 2
n = 2
a = [[2,-2],
[0,4]]
b = [[6,2],
[4,-5]]
c = [[0,0],
[0,0]]
print "How many rows and columns ?",m,n
print "Enter A matrix value"
for i in range(m):
for j in range(n):
print a[i][j],
print
print "Enter B matrix values"
for i in range(m):
for j in range(n):
print b[i][j],
print
# function to add matrices
def matadd():
for i in range(m):
for j in range(n):
c[i][j] = a[i][j] + b[i][j]
# call function
matadd()
print "Resultant matrix is"
for i in range(m):
for j in range(n):
print c[i][j],
print
# Program to multiply A matrix of order of m x n with B matrix of order n x l and print the resultant matrix
# Variable declaration
a = [[2,-2],
[0,4]]
b = [[6,2,],
[4,-5]]
c = [[0,0],
[0,0]]
m = 2
n = 2
l = 2
print "Enter order of A matrix (m x n) :",m,n
print "Enter A matrix values"
for i in range(m):
for j in range(n):
print a[i][j],
print
print "Enter order of B matrix (n x l) :",n,l
print "Enter B matrix values"
for i in range(m):
for j in range(n):
print b[i][j],
print
# function to multiply matrices
def matmul():
for i in range(m):
for j in range(l):
c[i][j] = 0
for k in range(n):
c[i][j] = c[i][j] + a[i][k] * b[k][j]
return c
# call function
matmul()
print "Resultant matix is"
for i in range(m):
for j in range(n):
print c[i][j],
print
# Program to transpose a matrix of order m x n
# Variable declaration
a = [[-3,6,0],
[3,2,8]]
at = [[0,0],
[0,0],
[0,0]]
m = 2
n = 3
ch = "y"
print "How many rows and columns ?",m,n
print "Enter the matrix values"
for i in range(m):
for j in range(n):
print a[i][j],
print
# function to transpose a matrix
def transpose(a,at,m,n):
for i in range(m):
for j in range(n):
at[j][i] = a[i][j]
return at
while ch == 'y' or ch == 'Y' :
# call function to transpose the matrix
transpose(a,at,m,n)
print "Transpose of the matrix is"
for i in range(n):
for j in range(m):
print at[i][j],
print
ch = "N"
print "Press y to continue"
print " any other key to stop.",ch
# Program to create a function to sort the elements of an array in ascending order
# Variable declaration
n = 4
x = [32,-10,20,5]
print "How many numbers ?",n
print "Enter the list of values"
for a in x:
print a,
print
# function to sort the numbers
def sort(x,n):
for i in range(n-1):
for j in range(i+1,n):
if x[i] > x[j]:
temp = x[i]
x[i] = x[j]
x[j] = temp
return x
# call function to sort the numbers
sort(x,n)
print "The sorted list is"
for a in x:
print a,
print