# Chapter 08: Functions in C¶

## Example 1 , Page number: CP-173¶

In :
# 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

Enter value to n and r : 5 3
Value of nCr = 10


## Example 2 , Page number: CP-174¶

In :
# 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

Enter three test scores : 62 70 58
Enter three assignment scores : 17 21 23
Total marks = 93


## Example 3 , Page number: CP-176¶

In :
# 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 "-----------------------------------"

----------------------------------
x in degrees        cos(x)
----------------------------------
0           1.00
30           0.87
60           0.50
90           0.00
120           -0.50
150           -0.87
180           -1.00
-----------------------------------


## Example 4 , Page number: CP-177¶

In :
# 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. . ."

Enter three integers : 45 27 81
Greatest common divisor is  9
Press any key to continue. . .


## Example 5 , Page number: CP-178¶

In :
# 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. . ."

Enter the integer : 2846
2846 is reversed as  6482
Press any key to continue. . .


## Example 6 , Page number: CP-179¶

In :
# 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)

Enter the first string : MUMBAI
Enter the second string : MYSORE
The result is  -1


## Example 7 , Page number: CP-181¶

In :
# 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)

How many values ? 6
Enter all values
3.1 3.8 3.6 4.0 3.4 3.8
Arithmetic mean = 3.62


## Example 8 , Page number: CP-182¶

In :
# 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)

How many rows and columns : 3 2
Enter the matrix values
2 3 4
2 0 1
Sum of all elements in the matrix = 12


## Example 9 , Page number: CP-183¶

In :
# 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)

Enter a string : NEW DELHI
NEW DELHI is reversed as  IHLED WEN


## Example 10 , Page number: CP-184¶

In :
# 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"

How many numbers ? 10
Enter all numbers in the list
-3 8 13 19 21 25 26 29 35 42
Enter the number to be searched : 19
The number 19 is present in the list


## Example 11 , Page number: CP-187¶

In :
# 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

Enter the values to n and r : 5 3
Value of nCr = 10


## Example 12 , Page number: CP-188¶

In :
# 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)

Enter value to x : 4.2
Enter its power : 3
4.20 raise to  3 is 74.09


## Example 13 , Page number: CP-189¶

In :
# 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)

How many terms to be printed ? 10
The first 10 terms in Fibonacci series are
0 1 1 2 3 5 8 13 21 34


## Example 14 , Page number: CP-196¶

In :
# 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


How many rows and columns ? 2 2
Enter A matrix value
2 -2
0 4
Enter B matrix values
6 2
4 -5
Resultant matrix is
8 0
4 -1


## Example 15 , Page number: CP-197¶

In :
# 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


Enter order of A matrix (m x n) : 2 2
Enter A matrix values
2 -2
0 4
Enter order of B matrix (n x l) : 2 2
Enter B matrix values
6 2
4 -5
Resultant matix is
4 14
16 -20


## Example 16 , Page number: CP-199¶

In :
# 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

How many rows and columns ? 2 3
Enter the matrix values
-3 6 0
3 2 8
Transpose of the matrix is
-3 3
6 2
0 8
Press y to continue
any other key to stop. N


## Example 17 , Page number: CP-200¶

In :
# 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

How many numbers ? 4
Enter the list of values
32 -10 20 5
The sorted list is
-10 5 20 32