Skip to content

Latest commit

 

History

History
98 lines (66 loc) · 2.66 KB

File metadata and controls

98 lines (66 loc) · 2.66 KB

max of two

The problem

Find the max number of two numbers.

Hints

Ask the user to enter two numbers.

Then, you can run a comparison to compare which one is larger.

Think about it and try yourself first.

Solution

num1 = int(input("First number: "))
num2 = int(input("Second number: "))
if (num2 >= num1):
    largest = num2
else:
    largest = num1
print("Largest number you entered is: ", largest)

Try it on Programming Hero

Shortcut

Another simple and alternative solution is to send the numbers to the max function.

num1 = int(input("First number: "))
num2 = int(input("Second number: "))
largest = max(num1, num2)
print("Largest number you entered is: ", largest)

Try it on Programming Hero

Alternative Solution

Another alternative approach is to use the floor method from the math module. If you pass a number with a fraction to the math.floor function, it will return you the integer part of the number.

For example,

import math
result = math.floor(3.4)
print(result)

Another way to solve the problem

So what we are going to do is to define a function that takes 2 parameters, a and b. We are using an if..else statement. If a is greater or less than b then a is the max number or else B is the max number.

Test Case-

Enter the first number: 10
Enter the second number: 9
Largest number you entered is 10
def maxof2Numbers( a, b):
    if a>=b:
        return a
    else:
        return b

a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))

largest = maxof2Numbers(a , b)
print(f"Largest number you entered is {largest}")

Try it on Programming Hero

Alternative Solution

Do you want to see your name in this app as a contributor?

If yes, find a better solution or alternative solution of any of the problems here. Then, send your solution with a code explanation to programming.hero1@gmail.com

If your solution is approved by the Team Programming Hero, we will publish your solution in the app with your name as the contributor.

That would be fun.

  Next Page  

tags: programming-hero python python3 problem-solving programming coding-challenge interview learn-python python-tutorial programming-exercises programming-challenges programming-fundamentals programming-contest python-coding-challenges python-problem-solving