Bhaskar baswala
Bhaskar baswala

Reputation: 35

Print largest & second largest number from three variables entered by the user by conditional statement

Hey I recently started to learn python. Below is the program to find the largest number, I want to know how do I print the second largest number. Thanks in advance.

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))

if x>y:
    f1=x
else:
    f1=y

if y>z:
    f2=y
else:
    f2=z
    
if f1>f2:
    print(str(f1)+" is greatest")
else:
    print(str(f2)+" is greatest")

Upvotes: 2

Views: 3929

Answers (7)

Aniket Barun
Aniket Barun

Reputation: 1

x, y, z = input().split()
x = int(x)
y = int(y)
z = int(z)

if (x-y) * (x-z) < 0:
    print(x)
elif (y-x) * (y-z) < 0:
    print(y)
else:
    print(z)

Upvotes: 0

sari
sari

Reputation: 23

You can use the same method but a little bit complicated. You can try this code:

x = int(input("Enter the 1st number: \n"))
y = int(input("Enter the 2nd number: \n"))
z = int(input("Enter the 3rd number: \n"))


if x < y and x > z or x < z and x > y:
    print("this is second largest number", str(x))


if y < x and y > z or y < z and y > x:
    print("this is second largest number", str(y))


if z < x and z > y or z < y and z > y:
    print("this is second largest number", str(z))

I personally wouldn't prefer this because it is too long, it works only with these 3 variabels, and wouldn't work for long numbers. But for a beginner it should be enough.

Upvotes: 1

Khalil
Khalil

Reputation: 1515

You can easily get the second largest number by adding the input to a list and sorting it.

Try this:

xyz = [x,y,z]
sorted_xyz = sorted(xyz)
largest = sorted_xyz[-1]
second_largest = sorted_xyz[-2]

Since you want this to work with conditional statements. You can try:

# check if x is the largest
if ( x >= y and x >= z ):
    print(str(x) + " is the greatest")
    # check if y is greater than z, if so then it is the second largest number.
    if ( y >= z ):
        print(str(y) + " is second greatest")
    else:
        print(str(z) + " is second greatest")

# check if y is the largest
if ( y >= x and y >= z ):
    print(str(y) + " is the greatest")
    # check if x is greater than z, if so then it is the second largest number.
    if ( x >= z ):
        print(str(x) + " is second greatest")
    else:
        print(str(z) + " is second greatest")

# check if z is the largest
if ( z >= x and z >= y ):
    print(str(z) + " is the greatest")
    # check if x is greater than y, if so then it is the second largest number.
    if ( x >= y ):
        print(str(x) + " is second greatest")
    else:
        print(str(y) + " is second greatest")

Upvotes: 2

VPfB
VPfB

Reputation: 17267

If you insist on conditionals:

if x <= y <= z or z <= y <= x:
    print(y)
elif y <= x <= z or z <= x <= y:
    print(x)
else:
    print(z)

The code is based on 6 possible ordering of 3 values (xyz, xzy, ...).

However, it works only for exactly 3 values and is not scalable.

Upvotes: 1

Vas
Vas

Reputation: 130

Instead of using booleans, use sorted:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))
print(f'Second largest number is {sorted([x,y,z])[-2]}')

This works for as many inputs

Upvotes: 1

XxJames07-
XxJames07-

Reputation: 1826

you can do something like this:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))

numbers_in_ascending_order = sorted([x,y,z])

numbers_in_descending_order = numbers_in_ascending_order.reverse()

second_largest = numbers_in_descending_order[1]

print("second largest:"+second_largest.__str__())

or you can try this:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))
lst = [x,y,z]
lst.sort(reverse=True)
second_greatest = lst[1]
print("second greatest:"+second_greatest.__str__())

Out:

>>> python3 test.py
Enter the 1st number:
3
Enter the 2nd number:
5
Enter the 3rd number:
12
second greatest:5

Upvotes: 1

alexzander
alexzander

Reputation: 1875

use min and max, which are python's builtins

x = 12
y = 5
z = 3
second_max = min(x, max(y, z))
print(second_max)

out

5

no matter the values, you will always get the second max.

Upvotes: 2

Related Questions