# Comparing two numbers

## Challenge

Given two integers A and B as input, you must write a program which outputs if A>B, A==B or A<B.

The integers will be in any reasonable range supported by your language which includes at least 256 values.

Your program can be either a full program or a function, taking input via STDIN or function arguments.

## Outputs

If A>B output

A is greater than B

If A==B output

A is equal to B

If A<B output

A is less than B

Where you replace A and B for their integer values.

For example:

A   B   Output
--  --   ------
0   1   0 is less than 1
5   2   5 is greater than 2
20  20   20 is equal to 20

## Winning

The shortest program in bytes wins.

• Today on Programming Puzzles & Code Golf: ternary statements! Sep 1, 2015 at 1:29
• Can functions simply return the solution instead of printing out the solution? Sep 2, 2015 at 14:18
• @TheNumberOne No, they must print the solution Sep 2, 2015 at 16:07

# Processing, 92 bytes

void c(int a,int b){print(a+" is "+(a>b?"greater than ":a<b?"lesser than ":"equal to ")+b);}

# Ruby 1.9+, 737170 68 bytes

->a,b{[a,b]*" is #{%w{equal\ to greater\ than less\ than}[a<=>b]} "}
• 1 character shorter with word array: %w{equal\ to greater\ than less\ than} Aug 31, 2015 at 12:10
• Can shave two more characters using Array#*: ->a,b{[a,b]*" is #{%w{equal\ to greater\ than less\ than}[a<=>b]} "} Aug 31, 2015 at 18:56

a!b|a>b="greater than "|a<b="less than "|1<3="equal to "
a#b=show a++" is "++a!b++show b

thanks to nimi

94 byte version:

d a|a>0="greater than "|a<0="less than "|1<3="equal to "
c a b=show a++" is "++d(a-b)++show b

97 byte version:

d a b|a>b=" greater than "|a<b=" less than "|1<3=" equal to "
c a b=show a++" is"++d a b++show b

my first time golfing :) (tested with ghci, size determined with du and stat)

• You can save a few bytes by turning the functions d and c into operators, e.g. d becomes a!b|a>b=... and c becomes a#b=show a++" is"++a!b++show b. Example run: 41#51.
– nimi
Aug 31, 2015 at 16:29
• You need one more fix: d(a-b) --> a!b, and this byte counter tells me your score is then 88.
– lynn
Sep 1, 2015 at 1:32
• Thx - i missed that Sep 1, 2015 at 6:06

# PHP, 103 100 bytes

103 byte version.

function c($a,$b){echo "A is ".($a==$b?"equal to":($a>$b?"greater than":($a<$b?"less than":"")))." B";}

100 byte version (thanks jrenk)

<?php function c($i,$u){echo$i.' is '.($i>$u?'greater than ':($i==$u?'equal to ':'less than ')).$u;}
• I think you should replace "A" and "B" in the output with the numbers you get from the input. Sep 1, 2015 at 6:56
• You should count the "<?php" opening tag to your byte count. You can save a few bytes by just using ":" for 'less than' since it's the last case that could happen. Look here. Sep 1, 2015 at 7:25
• Awesome. Thanks for your help! Sep 1, 2015 at 11:35
• you can shorten it by 1 byte if you use: echo"$i is " instead of echo$i.' is '
– Dan
Sep 1, 2015 at 15:34

# PHP, 88 bytes

PHP function, 88 bytes:

function a($a,$b){echo"$a is ",$a-$b?($a>$b?"greater":"less")." than":"equal to","$b";}
function b($a,$b){echo"$a is ",$a-$b?$a>$b?"greater than":"less than":"equal to","$b";}
function c($a,$b){echo"$a is ",$a-$b?($a>$b?"greater":"less")." than$b":"equal to $b";} Note than$a-$b can be replaced by$a^$b. Full program, 89 bytes: <?=$a=$argv[1]," is ",$a-($b=$argv[2])?($a>$b?"greater":"less")." than $b":"equal to$b";

I used:

# JavaScript, 84 bytes

f=function(a,b){alert(a+' is '+(a-b?(a>b?'greater':'less')+' than ':'equal to ')+b)}

# 05AB1E, 30 bytes

.S“ž‰€„†ª€ëîØ€ë“#2ôè¹'€ˆ.À²ðý

Try it online!

# Befunge-93, 82 bytes

<v"equal to "$_v#!\!0:\0:-&,,,"is ".::& v "less than "_" naht retaerg" @>:#,_$-.

Try it online!

# APL (Dyalog Unicode), 56 bytesSBCS

Anonymous infix lambda.

{∊⍺'is ','less than' 'equal to' 'greater than'[2+×⍺-⍵]⍵}

Try it online!

{} "dfn"; left argument is and right argument is :

the right argument to the right of
'less than' 'equal to' 'greater than'[] this list of three strings indexed by:
⍺-⍵ left argument minus right argument
× sign of that

⍺'is ', prepend the left argument and the string "is "

ϵnlist (flatten)

# Quipu, 154 bytes

'g'l'e\/' 3&5&2&[]1&' 4&
'r'e'q/\'i[][][]/\[]/\[]
'e's'u  's4&9&/\1@/\  /\
'a's'a  ' []<<1@??
't' 'l  /\--8&??
'e't'   \/  >>
'r'h't
' 'a'o
't'n
'h
'a
'n

Attempt This Online!

### Explanation

Threads 0 through 2 store the strings greater than, less than, and equal to, respectively. Then:

• Thread 3 inputs a number, stores it, and also outputs it
• Thread 4 outputs is , then inputs a number and stores it
• jumps to thread 9 if it is less than 0
• jumps to thread 8 if it is greater than 0
• continues to thread 7 otherwise
• Thread 10 outputs a space

# Vyxalṡ, 34 bytes

=[«ß‟₆nḭ«|<[∨⅛|∆ḭ]λ⊍]?^‛is?^

Try it Online!

=[«ß‟₆nḭ«|<[∨⅛|∆ḭ]λ⊍]?^‛is?^ # Takes 2 numbers as arguments
=[«ß‟₆nḭ«                  ]        # If equal, push the base compressed string "equal to"
|                          # Otherwise...
<[∨⅛     ]              # If less than, push the string "less"
|∆ḭ               # Otherwise, push the string "greater"
λ⊍          # Push the string "than"
?        # Push the first number (due to the input mode, this turns out to be the second input)
^       # Reverse the stack so we can prepend stuff
‛is    # Push the string "is"
?   # Push the second number (first input)
^  # Reverse the stack again
# Implicit output. ṡ joins the stack on spaces
• Try it Online! for 30 bytes Jun 22, 2023 at 13:11

# Thue, 181 bytes

1,1::=o,x
x1::=1x
x]::=]x
!x::=!o
11,]::=1o,]
[1,]::=!vo
vx::=>x
[,11::=[,1x
[,1]::=!<x
[,]::=!=
o::=~1
>::=~ is greater than
<::=~ is less than
=::=~ is equal to
*::=:::
::=
[*]

Try it online!

The TIO version contains a trailing newline, but that’s only needed in this specific implementation; the specification doesn’t specify that the trailing newline is needed and other interpreters (e.g.) don’t require it. Similarly, the newlines in the output don’t show up in the linked interpreter.

## IO Format

Input and output are in unary strings of 1s, and there is a comma (but no spaces) between A and B. e.g. For A = 2 and B = 3, input 11,111.

## Explanation (not really)

Unfortunately, my own motivation to write an explanation is inversely proportional to the reader’s desire for one, so I won’t write a detailed explanation. However, this ungolfed version that’s just split into sections might help (I’ll use # to denote comments):

# General stuff
1,1::=o,x
x1::=1x
x]::=]x
!x::=!o

# Check for greater than-ness
11,]::=1o,]
[1,]::=!vo
vx::=>x

# Check for less than-ness
[,11::=[,1x
[,1]::=!<x

# Check for equality
[,]::=!=

# Output
o::=~1
>::=~ is greater than
<::=~ is less than
=::=~ is equal to

# Input
*::=:::
::=
[*]

Look at the Input, Output, and General stuff sections first before looking at the other sections.

Also, maybe use this interpreter to step through some examples.

# Thue, 48 bytes, non-competing due to output format

*::=:::
1_1::=_
1__::=~>
__1::=~<
___::=~=
::=
*

Try it online!

### IO format

The numbers are inputted in unary as strings of 1s, with an underscore on either side and an underscore between them. For example, to compare 2 and 3, the input would be _11_111_.

If the input is _A_B_ then the output is > if $$\A>B\$$, < if $$\A, and = if $$\A=B\$$.

### Explanation

I will assume some familiarity with Thue, so go read the Language description section of the Esolang page.

1_1::=_     (Continually) subtract 1 from both numbers while both are positive.
1__::=~>    If B becomes 0 but A remains positive, A > B.
__1::=~<    If A becomes 0 but B remains positive, A < B.
___::=~=    If both A and B become 0, A = B.
::=
*
• TIO needs a trailing newline for some reason. Additionally, the description does seem rather strict on what the output should be i'm afraid
– Jo King
Feb 22, 2022 at 6:44
• @JoKing Oh, I didn't notice that. Thanks for pointing it out! Since the output format looks very nontrivial to implement (since I have to output the actual values of A and B), I'll just keep my code as it is and mark it as noncompeting for now until I get around to correcting my answer.
– user101133
Feb 22, 2022 at 19:28

# C++, 109 bytes

#include<cstdio>
int x(int a,int b){printf("%d is %s %d",a,a>b?"greater than":a<b?"less than":"equal to",b);}

# Inform 7, 109 bytes

To(A - value)T(B - value):say "[A] is [if A is B]equal to [else if A > B]greater than [else]less than ";say B

This defines a phrase used as 1 T 2;.

# Common Lisp, 92 bytes

(lambda(a b)(format t"~A is ~[less than~;equal to~;greater than~] ~A"a(1+(signum(- a b)))b))

(lambda(a b)
(format t "~A is ~[less than~;equal to~;greater than~] ~A"
a                   ; first ~A
(1+(signum(- a b))) ; gives 0, 1 or 2 which selects a clause in ~[ ~]
b                   ; second ~A
))
• Good, but reversed. I ran it like ((lambda(a b)(format t"~A is ~[less than~;equal to~;greater than~] ~A"a(1+(signum(- b a)))b)) 10 20) in CLISP console and it yields 10 is greater than 20. Aug 31, 2015 at 15:37
• @pawel.boczarski Thanks a lot. Aug 31, 2015 at 18:54

# PHP 7, 91 bytes

<?=$a=$argv[1],[~ß–ŒßšŽŠž“ß‹ß,~ß–Œß“šŒŒß‹—ž‘ß][($b=$argv[2])<=>$a]?:~ß–Œß˜šž‹šß‹—ž‘ß,$b;

### Usage:

Save as ANSI in file.php and run:

php -derror_reporting=~E_NOTICE file.php A B

Is suppressing notices via CLI flag considered cheating? If so, here is a version that works with full error reporting, (93 Bytes)

<?=$a=$argv[1],@[~ß–ŒßšŽŠž“ß‹ß,~ß–Œß“šŒŒß‹—ž‘ß][($b=$argv[2])<=>$a]?:" is greater than ",$b;

### Some Explanation

For the string constants, I used undefined constants and bitwise invert them with ~. PHP replaces undefined constants with the name of the constant (foo => "foo") and allows special characters in constant names. This way you can save a byte even for strings with whitespace. Example: => " ")

The spaceship operator <=> returns 0 if the operands are equal, -1 if the left operand is less than the right operand and 1 if the left operand is greater than the left operand. I use this as array index, to get " is equal to " and " is less than ". For the non-existing array key -1 the result is null and thanks to ?: we get " is greater than " in this case.

• As long as the errors aren't output to STDOUT, suppressing them is fine Aug 31, 2015 at 21:27

# JavaScript, 92 bytes

a=+prompt(),b=+prompt();alert(a+" is "+(a>b?"greater than ":a<b?"less than ":"equal to ")+b)

## Swift 2.0, 87 bytes

func g(a:Int,b:Int){print("A is",a==b ?"equal to":a<b ?"less than":"greater than","B")}

# PowerShell, 98 85 Bytes

(Thanks to tomkandy for the huge assist on the param and reminding me that PowerShell likes inline subexpression parsing, so go ahead and use it)

param($a,$b);"$a is$(@("equal to","greater than","less than")[$a.CompareTo($b)]) $b" Essentially the same, just trimmed down quite a bit. (Original below) Decently competitive, I was somewhat surprised. Again, the$ character and taking input is the downfall.

$a=$args[0];$b=$args[1];"$a is "+@("equal to","greater than","less than")[$a.CompareTo(+$b)]+"$b"

(Creating a function of two arguments is one byte longer than taking command-line input of two arguments, thanks to the } at the end)

function r{param($a,$b);<doing_stuff>}
$a=$args[0];$b=$args[1];<doing_stuff>

### Expanded:

$a=$args[0]
$b=$args[1]
"$a is " + @("equal to","greater than","less than")[$a.CompareTo(+$b)] + "$b"

Similar in concept to some other languages present, this is utilizing a feature of x.CompareTo(y), which returns -1 if x<y, 0 if x=y, and 1 if x>y. We take that result and pull out the appropriate value from a dynamic array we just created, leveraging the fact that [-1] stands for the last item in the array.

Do note the + inside the CompareTo(+$b) statement -- an explicit cast to an Integer. If we left that off, it would try to utilize the String comparison instead, which yields odd behavior. • You can get this down to 85 bytes with param - you don't need to use function to do so, param will work in a script - and by using subexpressions param($a,$b);"$a is $(@("equal to","greater than","less than")[$a.CompareTo($b)])$b" Sep 2, 2015 at 16:41
• Thanks @tomkandy ... I keep forgetting about the inline sub-expression expansion. And this is honestly the first I've come across using param not inside a function block; I've always stuck with $args ... guess that's what I get for being trained on VBScript and using objArg everywhere ... Sep 2, 2015 at 17:13 # Mouse-2002, 73 bytes Uses variables (lame, I know). ?a:?b:"A is "a.b.>["greater than"]a.b.<["less than"]a.b.=["equal to"]" B" Explained: ? a: ? b: ~ store input in a , b "A is " ~ print a. b. > [ ~ testif "greater than" ~ print ] a. b. < [ ~ rinse && repeat "less than" ] a. b. = [ "equal to" ] " B" ## Pure stack version: 96 95 bytes I had fun writing this one. It would be a LOT shorter if Mouse had a 2dup instruction, but I implemented it myself instead. ??#D;#D;"A is ">["greater than"]<["less than"]=["equal to"]" B"$D&swap &dup &rot &rot &over @

How this works:

? ? #D; #D; ~ get some input, then 2dup twice
"A is "                ~ print this
> [                  ~ test if gtr
"greater than"       ~ print this
]                      ~ fallthrough
< [                  ~ repeat for next two conditions
"less than"
]
= [
"equal to"
]
" B"

# PowerShell, 71 Bytes

param($a,$b)"$a is$($a-$b|% T*g "greater than;less than;equal to") \$b"

an update to the existing powershell answer from 2 years ago.

# J, 64 bytes

9;:inv@A.;&":,'is'~.@;cut@'equal to greater than less'{~1 2*>-<

Try it online!

equal to is joined by a non-break space.

1 2*>-<: –1, –2 if less; 0, 0 if equal; 1, 2 if greater than
;&":,'is'~.@; prepend is, then prepend A and B. Because 0, 0 yields equal to,equal to, there’s a ~. to remove duplicates.
9 A. to shuffle B to the last position.
;:inv@ join by spaces

# Swift >=2.2, 85 bytes

func f(a:Int,b:Int){print(a,"is",a==b ?"equal to":(a>b ?"greater":"less")+" than",b)}

The two current Swift answers, both in Swift 2.0, are invalid because they print the literal letters "A" and "B" instead of the values given into the function. Other than that, this is pretty much identical to those two.

I can't find any Swift 2.0 or 2.1 compilers online, so I can't say for certain it works in those versions, but I don't see why it wouldn't. The function is f(_:b:) in Swift 2.2 and f(a:b:) in Swift >=3.0.

# Noether, 91 79 bytes

I~aP" is "PI~b{ab=}{"equal to"P}{ab>}{"greater than"P}{ab<}{"less than"P}" "PbP

Try it here!

Note that while Noether is based heavily on Fourier, I have sacrificed golfability for functionality.

# 05AB1E, 38 36 bytes

²¹.S>ð“îØ€ë
ž‰€„
†ª€ë“«¶¡è¹… is«sð²J

I'm surprised there isn't already a 05AB1E answer. You can write almost any word in 05AB1E with just 2 bytes.

Try it online!