Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Program to Find HCF(Highest Common Factor)

def calculate_hcf(x, y):  

    if x > y:  

        smaller = y  

    else:  

        smaller = x  

    for i in range(1,smaller + 1):  

        if((x % i == 0) and (y % i == 0)):  

            hcf = i  

    return hcf  

num1 = int(input("Enter first number: "))  

num2 = int(input("Enter second number: "))  

print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))  

Program to find minimum cost path

R = 3

C = 3

def minCost(cost, m, n):

tc = [[0 for x in range(C)] for x in range(R)]


tc[0][0] = cost[0][0]

for i in range(1, m + 1):

tc[i][0] = tc[i-1][0] + cost[i][0]

for j in range(1, n + 1):

tc[0][j] = tc[0][j-1] + cost[0][j]

for i in range(1, m + 1):

for j in range(1, n + 1):

tc[i][j] = min(tc[i-1][j-1], tc[i-1][j],tc[i][j-1]) + cost[i][j]

return tc[m][n]

cost = [[1, 2, 3],

[4, 8, 2],

[1, 5, 3]]

print(minCost(cost, 2, 2))


Program to find LCM

def calculate_lcm(x, y):  

    if x > y:  

        greater = x  

    else:  

        greater = y  

    while(True):  

        if((greater % x == 0) and (greater % y == 0)):  

            lcm = greater  

            break  

        greater += 1  

    return lcm    

num1 = int(input("Enter first number: "))  

num2 = int(input("Enter second number: "))  

print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))  


Reverse of a number using functions

def intreverse(Number):
  Reverse = 0          # Initialize the reverse value to overcome garbage value storage
  while(Number > 0):
    Reminder = Number %10
    Reverse = (Reverse *10) + Reminder
    Number = Number//10
  return (Reverse)

Program to find even number using functions without return

def even(n):
      if  n%2==0:
            print 'Even number'
      else:
            print 'Odd Number'

n=int(raw_input('Enter the number to check even or not:'))
even(n)  # No return from function but the message is displayed inside the function itself

Simple Calculator using functions

def su(a,b):  
      c=a+b
      print 'Added value is:',c
     
def subt(a,b):
            if a>b:
                  c=a-b
                  print 'Subtracted value is:',c
            else:
                  c=b-a
                  print 'Subtracted value is:',c
                  
def mul(a,b):
      c=a*b
      print 'Multiplication value is:',c

def div(a,b):
      if b==0:         
            print 'Division by zero not defined'
      else:
            c=float(a)/b
            print 'Divison value is:',c

def exp(a,b):
      c=a**b
      print 'Exponent value is:',c


a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add  2.sub  3.mul  4. div  5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
      su(a,b)        # Function call
elif n==2:
      subt(a,b)    # Function call
elif n==3:
      mul(a,b)          # Function call
elif n==4:
      div(a,b)    # Function call
elif n==5:
      exp(a,b)    # Function call
else:
      print 'Invalid options'

Factorial using Recursion



def recur_factorial(n):
   if n == 1:
       return n
   else:
       return n*recur_factorial(n-1)


num = int(input("Enter a number: "))

if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   print("The factorial of",num,"is",recur_factorial(num))