Write A Lambda Function To Find Modulo Of Given Number

Hi Guys, welcome to my blog in this article you will learn about how to Write A Lambda Function To Find Modulo Of Given Number

 

Title : 

Write A Lambda Function To Find Modulo Of Given Number


Write A Lambda Function To Find Modulo Of Given Number



 

Description : 

Hey guys, in this article you will learn how to compute the modulo of a given number using the lambda function. Define a lambda function named as modulo() and pass the two arguments x and y just return the x % y.


    
modulo = lambda x, y: x % y


    


In the main program take two variables a and b as numerator and denominator, and ask the user to enter these two integers.


    
a = int(input("Enter Numerator : "))
b = int(input("Enter Denominator : "))


    


Once we get values of a and b use try and except block, in the try block call the modulo function by passing user input a and b. If there is zero division error except it with the exception and print some error message.


    
try:
   print(f"Modulo of {a} % {b} is : {modulo(a, b)}")
except ZeroDivisionError:
   print("Error : Cannot divide by zero")


    




Complete Python Code : 


    
modulo = lambda x, y: x % y

a = int(input("Enter Numerator : "))
b = int(input("Enter Denominator : "))

try:
   print(f"Modulo of {a} % {b} is : {modulo(a, b)}")
except ZeroDivisionError:
   print("Error : Cannot divide by zero")



    

 

 

Output : 


    
Enter Numerator : 30
Enter Denominator : 5
Modulo of 30 % 5 is : 0


    

 

Output : 


    
Enter Numerator : 33
Enter Denominator : 2
Modulo of 33 % 2 is : 1


Enter Numerator : 45
Enter Denominator : 0
Error : Cannot divide by zero


    



Conclusion :

The above Python program defines a lambda function that takes two integers as input to it, computes the value of first number by second number and returns the modulo as result. In the main program we will ask users to enter two integers and then call the modulo lambda function using try and except block to catch zero division error. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments