Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added another alternative solution of max-of-two-digits #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions Number-Related/max-of-two.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

# 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.
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.
Think about it and try yourself first.

## Solution

```python
num1 = int(input("First number: "))
num2 = int(input("Second number: "))
Expand All @@ -21,41 +23,76 @@ else:
largest = num1
print("Largest number you entered is: ", largest)
```

**[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**

## Shortcut

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

```python
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](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**

## 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,
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,

```python
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
```

```python
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](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**

## 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.
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](../assets/next-button.png)](Max-of-three.md)
 

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`

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`