← All questions
RareProgramming

Write a program that compares two integers and prints 1 if the first is greater, 2 if the second is greater, and 0 if they are equal.

Short answer
a > b → 1, a < b → 2, a = b → 0

Explanation

The output identifies the larger number by its position. If a is greater, print 1. If b is greater, print 2. When both comparisons are false the inputs are equal, which accounts for the final 0.

The split() call separates the input at whitespace, so a space or a line break works. Converting each entry with int makes comparisons depend on its numerical value.

import sys

a, b = map(int, sys.stdin.read().split())
if a > b:
    print(1)
elif b > a:
    print(2)
else:
    print(0)

Swap the two inputs. For unequal values the answer should switch between 1 and 2. Equal inputs still produce 0. A pair of negative inputs checks that the comparison uses signed values.

Input Check Output
8 3 8 > 3 1
3 8 3 < 8 2
7 7 7 = 7 0
−2 −7 −2 > −7 1
Conditionals

Platforms

  • MAXIMUM

Download now and try it for free on your own tasks!

10 15 free credits • No credit card required
Version: Loading…

Choose the Windows app or the Chrome extension with phone controls.