Print Smallest Of Three Integers Without Comparison Operator in Python
When writing any program, you must be aware of what you’re writing in your code. That way even if you get a pretty easy problem you can add your solution to make it look more creative. Doing so not only enhances your problem-solving skills but also increases creativity levels in you as a programmer. It might feel difficult to do so at first but as you keep solving different questions, you’ll soon be able to add your unique solution different from others. Therefore today we’re going to do the same. We’ll solve the problem of comparisons differently. So let’s find out how to print the Smallest Of Three Integers Without Using Comparison Operator in Python.
What’s The Approach?
- Though we can easily find the smallest number, however, the constraint is we cannot use any comparison operators. Therefore we will use subtraction to find the smallest integer.
- Let us consider input Integers
whileloop, we’ll pass these Integers as conditions till their value is greater than 0, with
x and y and z
- Inside the while loop, we’ll decrement each of our input Integers by one. At the same time, we’ll increment a new variable c by one.
- The smallest integer will become 0 first and its value will be the same as variable c, therefore we’ll simply print c as the smallest integer.
Also Read: Swap Integers Without Temporary Variable in Python
Python Program To Print Smallest Of Three Integers Without Using Comparison Operator
x = 12
y = 15
z = 5
Minimum of 3 numbers is 5
# Python program to find Smallest # of three integers without # comparison operators def smallest(x, y, z): c = 0 while ( x and y and z ): x = x-1 y = y-1 z = z-1 c = c + 1 return c # Driver Code x = 12 y = 15 z = 5 print("Minimum of 3 numbers is", smallest(x, y, z))