Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 2.06 KB

File metadata and controls

61 lines (45 loc) · 2.06 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)

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