Integer comparison: Difference between revisions
PascalABC.NET
(PascalABC.NET) |
|||
(25 intermediate revisions by 16 users not shown) | |||
Line 21:
=={{header|11l}}==
<
V b = Int(input(‘Enter value of b: ’))
Line 29:
print(‘a is greater than b’)
I a == b
print(‘a is equal to b’)</
=={{header|360 Assembly}}==
Input is done by a standard register 1 pointed parameter list.
<
* Reg1=Addr(Addr(argA),Addr(argB))
L 2,0(1) Reg2=Addr(argA)
Line 65:
A DS F 31-bit signed integer
B DS F 31-bit signed integer
END</
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR Compare).
Specific OS/hardware routines for user input and printing are left unimplemented.
It should be noted that this is strictly an unsigned comparison.
<syntaxhighlight lang="6502asm">Compare PHA ;push Accumulator onto stack
JSR GetUserInput ;routine not implemented
;integers to compare now in memory locations A and B
Line 84 ⟶ 85:
A_equals_B: JSR DisplayAEqualsB ;routine not implemented
Done: PLA ;restore Accumulator from stack
RTS ;return from subroutine</
=={{header|68000 Assembly}}==
The task of getting input from the user is much more difficult in assembly since it hasn't been done for you unlike nearly all other languages. So that part will be omitted to focus on the actual comparison.
In assembly, the only difference between a signed integer and an unsigned integer is the comparator(s) used to evaluate it. Therefore we have two different versions of this task.
===Unsigned Comparison===
<syntaxhighlight lang="68000devpac">Compare:
;integers to compare are in D0 and D1
CMP.L D1,D0
BCS .less ;D1 < D0
;else, carry clear, which implies greater than or equal to.
BNE .greater ;D1 > D0
;else, d1 = d0
LEA Equal,A0
JMP PrintString ;and use its RTS to return
.greater:
LEA GreaterThan,A0
JMP PrintString ;and use its RTS to return
.less:
LEA LessThan,A0
JMP PrintString ;and use its RTS to return
LessThan:
dc.b "second integer less than first",0
even
GreaterThan:
dc.b "second integer greater than first",0
even
Equal:
dc.b "both are equal",0
even</syntaxhighlight>
===Signed Comparison===
<syntaxhighlight lang="68000devpac">Compare:
;integers to compare are in D0 and D1
CMP.L D1,D0
BLT .less ;D1 < D0
;else, carry clear, which implies greater than or equal to.
BNE .greater ;D1 > D0
;else, d1 = d0
LEA Equal,A0
JMP PrintString ;and use its RTS to return
.greater:
LEA GreaterThan,A0
JMP PrintString ;and use its RTS to return
.less:
LEA LessThan,A0
JMP PrintString ;and use its RTS to return
LessThan:
dc.b "second integer less than first",0
even
GreaterThan:
dc.b "second integer greater than first",0
even
Equal:
dc.b "both are equal",0
even</syntaxhighlight>
=={{header|8051 Assembly}}==
Input/output is specific to hardware; this code assumes the integers are in registers a and b.
There is only one comparison instruction to use: 'not equal'.
<
push psw
cjne a, b, clt
Line 105 ⟶ 167:
compare_:
pop psw
ret</
Testing for (a <= b) or (a >= b) can be performed by changing the jumps.
=={{header|8th}}==
The user would put the numbers on the stack and then invoke 'compare':
<
: compare \ n m --
2dup n:= if "They are equal" . cr then
2dup n:< if "First less than second" . cr then
n:> if "First greater than second" . cr then ;
</syntaxhighlight>
Alternatively one could use the "n:cmp" word
Line 121 ⟶ 183:
{{works with|aarch64-linux-gnu-as/qemu-aarch64}}
Compare once (<code>cmp x0, x1</code>) and use conditional branching (<code>b.eq</code> and <code>b.gt</code>).
<
.equ SVC_WRITE, 64
.equ SVC_EXIT, 93
Line 184 ⟶ 246:
_exit:
mov x8, #SVC_EXIT
svc #0</
=={{header|ABAP}}==
This works in ABAP version 7.40 and above. Note that empty input is evaluated to 0.
<syntaxhighlight lang="abap">
report z_integer_comparison.
Line 203 ⟶ 265:
write comparison_result.
</syntaxhighlight>
=={{header|Action!}}==
<
INT a,b
Line 221 ⟶ 283:
PrintF("%I is equal to %I%E",a,b)
FI
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Integer_comparison.png Screenshot from Atari 8-bit computer]
Line 231 ⟶ 293:
=={{header|Ada}}==
<
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
Line 254 ⟶ 316:
Put_Line("A is greater than B");
end if;
end Compare_Ints;</
=={{header|Aime}}==
<
output(integer a, integer b, text condition)
{
Line 283 ⟶ 345:
return 0;
}</
Run as:
<pre>aime FILE integer a 33 integer b 133</pre>
Line 301 ⟶ 363:
The above distributions of both [[ALGOL 68G]] and [[ELLA ALGOL 68]] compilers only
allow [[wp:ASCII|ASCII]] characters (ASCII has neither "≤", "≥" nor "≠" characters).
<
INT a, b;
read((a, space, b, new line));
Line 321 ⟶ 383:
print((a," is greater or equal to ", b, new line))
FI
)</
{{out}}
<pre>
Line 330 ⟶ 392:
=={{header|ALGOL W}}==
<
integer a, b;
Line 343 ⟶ 405:
if a > b then write( a, " is greater than ", b );
end.</
=={{header|AppleScript}}==
<
set n2 to text returned of (display dialog "Enter the second number:" default answer "") as integer
set msg to {n1}
Line 357 ⟶ 419:
end if
set end of msg to n2
return msg as string</
Or...
<
set n2 to text returned of (display dialog "Enter the second number:" default answer "") as integer
if n1 < n2 then return "" & n1 & " is less than " & n2
if n1 = n2 then return "" & n1 & " is equal to " & n2
if n1 > n2 then return "" & n1 & " is greater than " & n2</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 552 ⟶ 614:
szMessErrDep: .asciz "Too large: overflow 32 bits.\n"
</syntaxhighlight>
=={{header|Arturo}}==
<
b: to :integer input "enter a value for b: "
if a<b [ print [ a "is less than" b ] ]
if a>b [ print [ a "is greater than" b ] ]
if a=b [ print [ a "is equal to" b ] ]</
=={{header|Astro}}==
<
let b = input('Enter value of b: ')
Line 571 ⟶ 633:
print 'a is greater than b'
elif a == b:
print 'a is equal to b'</
=={{header|AutoHotkey}}==
Error checking is performed automatically by attaching UpDowns to each of the Edit controls. UpDown controls always yield an in-range number, even when the user has typed something non-numeric or out-of-range in the Edit control.
The default range is 0 to 100.
<
Gui, Add, UpDown, vVar1
Gui, Add, Edit
Line 595 ⟶ 657:
GuiClose:
ExitApp</
=={{header|Avail}}==
<
a ::= next line from standard input→integer;
b ::= next line from standard input→integer;
If a > b then [Print: "a > b";]
else if a < b then [Print: "a < b";]
else if a = b then [Print: "a = b";];</
=={{header|AWK}}==
<
if ($1 == $2) print $1, "is equal to", $2
if ($1 < $2) print $1, "is less than", $2
if ($1 > $2) print $1, "is greater than", $2
}</
In awk, a double equals symbol is required to test for equality.
A single equals sign is used for assignment, and will cause a bug if it is used within a boolean expression:
<
IF (n=3) PRINT "n is equal to 3" # The incorrectly used equals sign will set n to a value of 3</
=={{header|Axe}}==
<
If r₁<r₂
Disp "LESS THAN",i
Line 629 ⟶ 691:
Disp "GREATER THAN",i
End
Return</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic">10 INPUT "ENTER TWO INTEGERS: "; A%, B%
20 A$(0) = "NOT "
30 PRINT A% " IS " A$(A% < B%) "LESS THAN " B%
40 PRINT A% " IS " A$(A% = B%) "EQUAL TO " B%
50 PRINT A% " IS " A$(A% > B%) "GREATER THAN " B%</syntaxhighlight>
==={{header|BaCon}}===
<
INPUT "Enter first number " ,a
INPUT "Enter second number " ,b
IF a < b THEN PRINT a ," is less than ", b
IF a = b THEN PRINT a, " is equal to ", b
IF a > b THEN PRINT a, " is greater than ", b</
{{works with|QuickBasic|4.5}}
<
INPUT "a, b"; a, b 'remember to type the comma when you give the numbers
PRINT "a is ";
Line 649 ⟶ 716:
IF a = b THEN PRINT "equal to ";
IF a > b THEN PRINT "greater than ";
PRINT "b"</
==={{header|
<syntaxhighlight lang="freebasic">input "Please enter one integer: ", x
input "and a second integer: ", y
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> INPUT "Enter two numbers separated by a comma: " a, b
CASE TRUE OF
WHEN a < b: PRINT ;a " is less than "; b
WHEN a = b: PRINT ;a " is equal to "; b
WHEN a > b: PRINT ;a " is greater than "; b
ENDCASE</syntaxhighlight>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
Dim As Integer x, y
Input "Please enter two integers, separated by a comma : ", x , y
If x < y Then
Print x; " is less than "; y
End If
If x = y Then
Print x; " is equal to "; y
End If
If x > y Then
Print x; " is greater than "; y
End If
Print
Print "Press any key to exit"
Sleep</syntaxhighlight>
==={{header|FutureBasic}}===
Note: Strictly speaking, it's preferable to use "==" when comparing integers as seen in this example. While the "=" sign will work as a comparison in most cases, technically it should be used for assignment, i.e. a = 3 when a is assigned the value of 3, as contrasted with a == 3, where the value of a is being compared with 3. FB will flag a warning when "==" is used to compare two single, doubles or floats since comparing real numbers can be inaccurate.
<syntaxhighlight lang="futurebasic">_window = 1
begin enum 1
_integer1Fld
_integer2Fld
_compareBtn
_messageLabel
end enum
local fn BuildWindow
window _window, @"Integer Comparison", (0,0,356,85)
textfield _integer1Fld,,, (20,44,112,21)
TextFieldSetPlaceholderString( _integer1Fld, @"Integer 1" )
textfield _integer2Fld,,, (140,44,112,21)
TextFieldSetPlaceholderString( _integer2Fld, @"Integer 2" )
button _compareBtn,,, @"Compare", (253,38,90,32)
textlabel _messageLabel,, (18,20,320,16)
ControlSetAlignment( _messageLabel, NSTextAlignmentCenter )
end fn
local fn DoDialog( ev as long, tag as long )
long int1, int2
select ( ev )
case _btnClick
select ( tag )
case _compareBtn
int1 = fn ControlIntegerValue( _integer1Fld )
int2 = fn ControlIntegerValue( _integer2Fld )
if ( int1 < int2 ) then textlabel _messageLabel, @"The first integer is less than the second integer."
if ( int1 == int2 ) then textlabel _messageLabel, @"The first integer is equal to the second integer."
if ( int1 > int2 ) then textlabel _messageLabel, @"The first integer is greater than the second integer."
end select
case _controlTextDidChange
textlabel _messageLabel, @""
end select
end fn
fn BuildWindow
on dialog fn DoDialog
HandleEvents</syntaxhighlight>
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim sIn As String = InputBox("Enter 2 integers seperated by a comma")
Dim iFirst, iSecond As Integer
iFirst = Val(Split(sIn)[0])
iSecond = Val(Split(sIn)[1])
If iFirst < iSecond Then Print iFirst & " is smaller than " & iSecond & gb.NewLine
If iFirst > iSecond Then Print iFirst & " is greater than " & iSecond & gb.NewLine
If iFirst = iSecond Then Print iFirst & " is equal to " & iSecond
End</syntaxhighlight>
Output:
<pre>
21 is greater than 18
</pre>
==={{header|IS-BASIC}}===
<
110 INPUT PROMPT "Enter B: ":B
120 IF A<B THEN
Line 667 ⟶ 834:
160 ELSE
170 PRINT A;"is greater than ";B
180 END IF</
or
<
110 INPUT PROMPT "Enter B: ":B
120 SELECT CASE A
Line 680 ⟶ 847:
170 CASE ELSE
180 PRINT A;"is greater than ";B
190 END SELECT</
==={{header|
Verbose version:
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">input "Enter an integer for a. ";a
input "Enter an integer for b. ";b
'a=int(a):b=int(b) ???
print "Conditional evaluation."
if
if a=b then print "a=b " ; a ; " = " ; b
if a>b then print "a>b " ; a ; " > " ; b
print "Select case evaluation."
select case
case (a<b)
print "a<b " ; a ; " < " ; b
case (a=b)
print "a=b " ; a ; " = " ; b
case (a>b)
print "a>b " ; a ; " > " ; b
end select
</syntaxhighlight>
Concise:
<syntaxhighlight lang="lb">input "Enter an integer for a. ";a
input "Enter an integer for b. ";b
for i = 1 to 3
op$=word$("< = >", i)
if eval("a"+op$+"b") then print "a"+op$+"b " ; a;" ";op$;" ";b
next
</syntaxhighlight>
==={{header|OxygenBasic}}===
<syntaxhighlight lang="text">
uses console
print "Integer comparison. (press ctrl-c to exit) " cr cr
Line 706 ⟶ 898:
endif
loop
</syntaxhighlight>
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">If OpenConsole()
Print("Enter an integer: ")
x.i = Val(Input())
Print("Enter another integer: ")
y.i = Val(Input())
If x < y
Print( "The first integer is less than the second integer.")
ElseIf x = y
Print("The first integer is equal to the second integer.")
ElseIf x > y
Print("The first integer is greater than the second integer.")
EndIf
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</syntaxhighlight>
==={{header|QBasic}}===
<
IF x < y THEN PRINT x; " is less than "; y
IF x = y THEN PRINT x; " is equal to "; y
IF x > y THEN PRINT x; " is greater than "; y</
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">input "1st number:"; n1
input "2nd number:"; n2
if n1 < n2 then print "1st number ";n1;" is less than 2nd number";n2
if n1 > n2 then print "1st number ";n1;" is greater than 2nd number";n2
if n1 = n2 then print "1st number ";n1;" is equal to 2nd number";n2</syntaxhighlight>
==={{header|TI-83 BASIC}}===
<syntaxhighlight lang="ti83b">Prompt A,B
If A<B: Disp "A SMALLER B"
If A>B: Disp "A GREATER B"
If A=B: Disp "A EQUAL B"</syntaxhighlight>
==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">Local a, b, result
Prompt a, b
If a < b Then
"<" → result
ElseIf a = b Then
"=" → result
ElseIf a > b Then
">" → result
Else
"???" → result
EndIf
Disp string(a) & " " & result & " " & string(b)</syntaxhighlight>
==={{Header|Tiny BASIC}}===
{{works with|TinyBasic}}
Some implementations of Tiny BASIC use <code>,</code> instead of <code>;</code> for concatenation of print items.
<syntaxhighlight lang="basic">10 REM Integer comparison
20 PRINT "Enter a number"
30 INPUT A
40 PRINT "Enter another number"
50 INPUT B
60 IF A < B THEN PRINT A;" is less than ";B
70 IF A > B THEN PRINT A;" is greater than ";B
80 IF A = B THEN PRINT A;" is equal to ";B
90 END</syntaxhighlight>
==={{header|True BASIC}}===
<
INPUT x, y
Line 722 ⟶ 976:
IF x = y THEN PRINT x; " is equal to "; y
IF x > y THEN PRINT x; " is greater than "; y
END</
==={{header|VBA}}===
<syntaxhighlight lang="vb">Public Sub integer_comparison()
first_integer = CInt(InputBox("Give me an integer."))
second_integer = CInt(InputBox("Give me another integer."))
Debug.Print IIf(first_integer < second_integer, "first integer is smaller than second integer", "first integer is not smaller than second integer")
Debug.Print IIf(first_integer = second_integer, "first integer is equal to second integer", "first integer is not equal to second integer")
Debug.Print IIf(first_integer > second_integer, "first integer is bigger than second integer", "first integer is not bigger than second integer")
End Sub</syntaxhighlight>
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
option explicit
function eef( b, r1, r2 )
if b then
eef = r1
else
eef = r2
end if
end function
dim a, b
wscript.stdout.write "First integer: "
a = cint(wscript.stdin.readline) 'force to integer
wscript.stdout.write "Second integer: "
b = cint(wscript.stdin.readline) 'force to integer
wscript.stdout.write "First integer is "
if a < b then wscript.stdout.write "less than "
if a = b then wscript.stdout.write "equal to "
if a > b then wscript.stdout.write "greater than "
wscript.echo "Second integer."
wscript.stdout.write "First integer is " & _
eef( a < b, "less than ", _
eef( a = b, "equal to ", _
eef( a > b, "greater than ", vbnullstring ) ) ) & "Second integer."
</syntaxhighlight>
==={{header|Visual Basic .NET}}===
'''Platform:''' [[.NET]]
{{works with|Visual Basic .NET|9.0+}}
<syntaxhighlight lang="vbnet">Sub Main()
Dim a = CInt(Console.ReadLine)
Dim b = CInt(Console.ReadLine)
'Using if statements
If a < b Then Console.WriteLine("a is less than b")
If a = b Then Console.WriteLine("a equals b")
If a > b Then Console.WriteLine("a is greater than b")
'Using Case
Select Case a
Case Is < b
Console.WriteLine("a is less than b")
Case b
Console.WriteLine("a equals b")
Case Is > b
Console.WriteLine("a is greater than b")
End Select
End Sub</syntaxhighlight>
==={{header|Yabasic}}===
<
if x < y then print x, " is less than ", y : fi
if x = y then print x, " is equal to ", y : fi
if x > y then print x, " is greater than ", y : fi</
==={{
<syntaxhighlight lang="zxbasic">10 INPUT "Enter two integers: ";a;" ";b
20 PRINT a;" is ";("less than " AND (a<b));("equal to " AND (a=b));("greather than " AND (a>b));b</syntaxhighlight>
=={{header|Batch File}}==
<
setlocal EnableDelayedExpansion
set /p a="A: "
Line 751 ⟶ 1,066:
) else ( if %a% EQU %b% (
echo %a% is equal to %b%
)))</
{{Out}}
<pre>C:\Test>IntegerComparison.bat
Line 757 ⟶ 1,072:
B: 3
5 is greater than 3</pre>
=={{header|bc}}==
{{Works with|GNU bc}}
(POSIX bc doesn't have I/O functions/statements (i.e. <code>read</code> and <code>print</code>) but the rest of the code would work.)
<
b = read()
if (a < b) print "a is smaller than b\n"
if (a > b) print "a is greater than b\n"
if (a == b) print "a is equal to b\n"
quit</
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
let start() be
$( let a = ? and b = ?
writes("A? ") ; a := readn()
writes("B? ") ; b := readn()
writes(
a < b -> "A is less than B*N",
a = b -> "A is equal to B*N",
a > b -> "A is greater than B*N",
"Huh?!"
)
$)</syntaxhighlight>
=={{header|Befunge}}==
Line 780 ⟶ 1,103:
The branch commands (underline _ and pipe |) test for zero.
<
>&&"=A",,\:."=B ",,,\: .55+,-:0`|
v "<" _v#<
@,+55,," B",,,"A " < "=" <</
=={{header|BQN}}==
The function <code>Comp</code> compares two integers and returns the appropriate string result.
<
⊑(/⟜⟨ "Greater than" "Equal To" "Lesser Than" ⟩>∾=∾<)
Line 796 ⟶ 1,119:
"Equal To"
6 Comp 5
"Greater than"</
=={{header|Bracmat}}==
<
& get$:?B
& (!A:!B&out$"A equals B"|)
& (!A:<!B&out$"A is less than B"|)
& (!A:>!B&out$"A is greater than B"|);</
=={{header|Brat}}==
<
second = ask("Second integer: ").to_i
when { first > second } { p "#{first} is greater than #{second}" }
{ first < second } { p "#{first} is less than #{second}" }
{ first == second } { p "#{first} is equal to #{second}" }</
=={{header|Burlesque}}==
<
blsq ) "5 6"ps^pcm+.{"The first one is less than the second one""They are both equal""The second one is less than the first one"}\/!!sh
The first one is less than the second one
Line 823 ⟶ 1,146:
blsq ) "6 5"ps^pcm+.{"The first one is less than the second one""They are both equal""The second one is less than the first one"}\/!!sh
The second one is less than the first one
</syntaxhighlight>
=={{header|C}}==
<
int main()
Line 843 ⟶ 1,166:
return 0;
}</
=={{header|C sharp|C#}}==
<
class Program
Line 861 ⟶ 1,184:
Console.WriteLine("{0} is greater than {1}", a, b);
}
}</
=={{header|C++}}==
<
int main()
Line 886 ⟶ 1,209:
if (a > b)
std::cout << a << " is greater than " << b << "\n";
}</
=={{header|CFEngine}}==
<
bundle agent __main__
{
Line 905 ⟶ 1,228:
if => isgreaterthan( "$(first_integer)", "$(second_integer)" );
}
</syntaxhighlight>
=={{header|ChucK}}==
<syntaxhighlight lang="text">
fun void intComparison (int one, int two)
{
Line 917 ⟶ 1,240:
// uncomment next line and change values to test
// intComparison (2,4);
</syntaxhighlight>
=={{header|Clean}}==
<
compare a b
Line 931 ⟶ 1,254:
(_, a, console) = freadi console
(_, b, console) = freadi console
= compare a b</
=={{header|Clipper}}==
<
IF a < b
? "A is less than B"
Line 943 ⟶ 1,266:
ENDIF
Return Nil
</syntaxhighlight>
=={{header|Clojure}}==
Line 949 ⟶ 1,272:
It evaluates the when/println body three times, each time with op and string bound to their corresponding entries in the list of three operator/string pairs.
Note that this does no validation on input: if the user inputs a string then an exception will be thrown.
<
(doseq [[op string] [[< "less than"]
[> "greater than"]
[= "equal to"]]]
(when (op a b)
(println (str a " is " string " " b)))))</
=={{header|CMake}}==
<
# cmake -DA=3 -DB=5 -P compare.cmake
Line 975 ⟶ 1,298:
if(A GREATER B)
message(STATUS "${A} is greater than ${B}")
endif()</
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
PROGRAM-ID. Int-Compare.
Line 1,004 ⟶ 1,327:
GOBACK
.</
=={{header|ColdFusion}}==
Line 1,017 ⟶ 1,340:
===In CFML===
<
<cfargument name="Integer1" type="numeric">
<cfargument name="Integer2" type="numeric">
Line 1,040 ⟶ 1,363:
</cfif>
<cfreturn VARIABLES.Result >
</cffunction></
===In CFScript===
<
function CompareInteger( Integer1, Integer2 ) {
VARIABLES.Result = "";
Line 1,067 ⟶ 1,390:
return VARIABLES.Result;
}
</cfscript></
=={{header|Common Lisp}}==
You can type this directly into a REPL:
<
(b (read *standard-input*)))
(cond
Line 1,080 ⟶ 1,403:
((> a b) (format t "~A is greater than ~A." a b))
((= a b) (format t "~A is equal to ~A." a b))
(t (format t "Cannot determine relevance between ~A and ~B!" a b)))))</
After hitting enter, the REPL is expecting the two numbers right away.
Line 1,086 ⟶ 1,409:
Alternatively, you can wrap this code in a function definition:
<
(let ((a (read *standard-input*))
(b (read *standard-input*)))
Line 1,095 ⟶ 1,418:
((> a b) (format t "~A is greater than ~A." a b))
((= a b) (format t "~A is equal to ~A." a b))
(t (format t "Cannot determine relevance between ~A and ~B!" a b)))))</
Then, execute the function for better control:
Line 1,104 ⟶ 1,427:
If you run this program, it will halt awaiting user input. Toggle in the value of <math>x</math>, then click <tt>Enter</tt>, then toggle in <math>y</math>, then <tt>Enter</tt>, and then <tt>Run</tt>. <math>x</math> and <math>y</math> must both be unsigned eight-bit integers. The computer will halt with the accumulator storing 1 if <math>x</math>><math>y</math>, 0 if <math>x</math>=<math>y</math>, or -1 if <math>x</math><<math>y</math>; and it will be ready for a fresh pair of integers to be entered.
<
x: NOP
Line 1,131 ⟶ 1,454:
JMP start
one: 1</
=={{header|D}}==
<
import std.stdio, std.conv, std.string;
Line 1,151 ⟶ 1,474:
if (a > b)
writeln(a, " is greater than ", b);
}</
{{out}}
<pre>10 is less than 20</pre>
=={{header|Dc}}==
<
[Enter two numbers to compare: ]P
? sbsa
Line 1,163 ⟶ 1,486:
[ [equal to]]sp lb la =p
la n [ is ]P P [ ]P lbn []pP
</syntaxhighlight>
{{out}}
44 is greater than 3
=={{header|DCL}}==
<
$ inquire b "Please provide another"
$ if a .lt. b then $ write sys$output "the first integer is less"
$ if a .eq. b then $ write sys$output "the integers have the same value"
$ if a .gt. b then $ write sys$output "the first integer is greater"</
{{out}}
<pre>$ @integer_comparison
Line 1,190 ⟶ 1,513:
:''Slightly different than the [[#Pascal|Pascal]] example''
<
{$APPTYPE CONSOLE}
Line 1,201 ⟶ 1,524:
if a = b then Writeln(a, ' is equal to ', b);
if a > b then Writeln(a, ' is greater than ', b);
end.</
=={{header|Draco}}==
<
int a, b;
write("Please enter two integers: ");
Line 1,212 ⟶ 1,535:
elif a>b then writeln(a, " > ", b)
fi
corp</
=={{header|Dyalect}}==
Line 1,218 ⟶ 1,541:
{{trans|Clipper}}
<
if a < b {
"A is less than B"
Line 1,226 ⟶ 1,549:
"A equals B"
}
}</
=={{header|E}}==
<
println(if (a < b) { `$a < $b` } \
else if (a <=> b) { `$a = $b` } \
else if (a > b) { `$a > $b` } \
else { `You're calling that an integer?` })
}</
=={{header|EasyLang}}==
<syntaxhighlight lang="text">a = number input
b = number input
if a < b
Line 1,247 ⟶ 1,570:
if a > b
print "greater"
.</
=={{header|ECL}}==
<syntaxhighlight lang="ecl">
CompareThem(INTEGER A,INTEGER B) := FUNCTION
Result := A <=> B;
Line 1,260 ⟶ 1,583:
CompareThem(2,2); //Shows "2 is equal to 2"
CompareThem(2,1); //Shows "2 is greater than 1"
</syntaxhighlight>
=={{header|EDSAC order code}}==
The EDSAC offers two conditional branching orders, <tt>E</tt> (branch if the accumulator >= 0) and <tt>G</tt> (branch if the accumulator < 0). Testing for equality thus requires two operations.
<
==================
Line 1,318 ⟶ 1,641:
[ 18 ] AF [ - character ]
EZPF [ begin execution ]</
=={{header|Efene}}==
Line 1,324 ⟶ 1,647:
since if does pattern matching the else is required to avoid the application from crashing
<
if A == B {
io.format("~p equals ~p~n", [A, B])
Line 1,353 ⟶ 1,676:
compare(4, 5)
}
</syntaxhighlight>
=={{header|Eiffel}}==
<
APPLICATION
inherit
Line 1,385 ⟶ 1,708:
end
end
end</
=={{header|Elena}}==
ELENA 4.x:
<
public program()
Line 1,404 ⟶ 1,727:
if (a > b)
{ console.printLine(a," is greater than ",b) }
}</
=={{header|Elixir}}==
<
{b,_} = IO.gets("Enter your second integer: ") |> Integer.parse
Line 1,417 ⟶ 1,740:
a == b ->
IO.puts "#{a} is equal to #{b}"
end</
=={{header|Emacs Lisp}}==
<
"Compare A to B and print the outcome in the message buffer."
(interactive "nFirst integer ⇒\nnSecond integer ⇒")
Line 1,427 ⟶ 1,750:
((< a b) (message "%d is less than %d." a b))
((> a b) (message "%d is greater than %d." a b))
((= a b) (message "%d is equal to %d." a b))))</
Invoke from within Emacs Lisp (or e.g., with <code>M-:</code>) as <code>(integer-comparison 12 42)</code>
Or, use <code>M-x integer-comparison RET</code> and you'll be prompted for the two numbers.
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun main = int by List args
int a, b
if args.length > 1
a = int!args[0]
b = int!args[1]
else
a = ask(int, "Enter the first integer ")
b = ask(int, "Enter the second integer ")
end
writeLine("=== a <> b is " + (a <> b) + " ===")
if a < b do writeLine(a + " < " + b) end
if a == b do writeLine(a + " == " + b) end
if a > b do writeLine(a + " > " + b) end
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\IntegerComparison.emal
Enter the first integer 42
Enter the second integer 64
=== a <> b is -1 ===
42 < 64
</pre>
=={{header|Erlang}}==
<
main() ->
{ok, [N]} = io:fread("First integer: ", "~d"),
Line 1,452 ⟶ 1,803:
end.
</syntaxhighlight>
=={{header|Euphoria}}==
<
integer a,b
Line 1,469 ⟶ 1,820:
puts(1,"grater then")
end if
puts(1," b")</
=={{header|Excel}}==
Line 1,475 ⟶ 1,826:
Let's say you type in the values in cells A1 and B1, in C1, type in the following in a MS Excel 2010 sheet:
<
=IF($A1>$B1;concatenate($A1;" is greater than ";$B1);IF($A1<$B1;concatenate($A1;" is smaller than ";$B1);concatenate($A1;" is equal to ";$B1)))
</syntaxhighlight>
In a Google Docs spreadsheet, that becomes :
<
=IF($A1>$B1,concatenate($A1," is greater than ",$B1),IF($A1<$B1,concatenate($A1," is smaller than ",$B1),concatenate($A1," is equal to ",$B1)))
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
let r =
match a with
Line 1,493 ⟶ 1,844:
| x when x > b -> 1, printfn "%d is greater than %d" x b
| x -> 0, printf "default condition (not reached)"
fst r</
=={{header|Factor}}==
<
readln readln [ string>number ] bi@
[ > [ "A > B" print ] when ]
[ < [ "A < B" print ] when ]
[ = [ "A = B" print ] when ] 2tri ;
</syntaxhighlight>
=={{header|FALSE}}==
Only equals and greater than are available.
<
>[\$," is greater than "\$,]?
\>[\$," is less than "\$,]?
=["characters are equal"]?</
=={{header|Fantom}}==
Line 1,514 ⟶ 1,865:
Uses Env.cur to access stdin and stdout.
<
{
public static Void main ()
Line 1,536 ⟶ 1,887:
}
}
</syntaxhighlight>
=={{header|Fermat}}==
<
?a;
?b;
Line 1,545 ⟶ 1,896:
if a<b then !'Less than' fi;
if a>b then !'Greater than' fi;
.;</
=={{header|Fish}}==
This example assumes you [http://www.esolangs.org/wiki/Fish#Input.2Foutput pre-populate] the stack with the two integers.
<
v <>l?^"Please pre-populate the stack with the two integers."ar>l?^;
\$:@@:@)?v v ;oanv!!!?<
Line 1,561 ⟶ 1,912:
> v
/oo". "nooooo" and "n$< v o<
>"They're not equal, not greater than and not smaller than eachother... strange."ar>l?^;</
The last three lines aren't really needed, because it will never become true :P but I included them to show a way to do some error checking.
Line 1,568 ⟶ 1,919:
To keep the example simple, the word takes the two numbers from the stack.
<
2dup < if ." a is less than b" then
2dup > if ." a is greater than b" then
= if ." a is equal to b" then ;</
=={{header|Fortran}}==
In ALL Fortran versions (including original 1950's era) you could use an "arithmetic IF" statement to compare using subtraction:
<
integer a, b
Line 1,591 ⟶ 1,942:
40 continue
end</
In ANSI FORTRAN 66 or later you could use relational operators (.lt., .gt., .eq., etc.) and unstructured IF statements:
<
integer a, b
c fortran 77 I/O statements, for simplicity
Line 1,602 ⟶ 1,953:
if (a .eq. b) write(*, *) a, ' is equal to ', b
if (a .gt. b) write(*, *) a, ' is greater than ', b
end</
In ANSI FORTRAN 77 or later you can use relational operators and structured IF statements:
<
integer a, b
read(*,*) a, b
Line 1,617 ⟶ 1,968:
end if
end</
In ISO Fortran 90 or later you can use symbolic relational operators (<, >, ==, etc.)
<
integer :: a, b
read(*,*) a, b
Line 1,632 ⟶ 1,983:
end if
end program compare</
=={{header|friendly interactive shell}}==
<
read b
Line 1,666 ⟶ 1,995:
else if test $a -eq $b
echo Equal
end</
=={{header|Frink}}==
All integers in Frink can be arbitrarily large.
<syntaxhighlight lang="frink">[a,b] = eval[input["Enter numbers",["a","b"]]]
if a<b
println["$a < $b"]
Line 1,678 ⟶ 2,006:
println["$a == $b"]
if a>b
println["$a > $b"]</syntaxhighlight>
=={{header|FunL}}==
<
a = readInt()
Line 1,689 ⟶ 2,016:
val (_, c) = [((<), 'less than'), ((==), 'equal to'), ((>), 'greater than')].find( (compare, _) -> compare(a, b) ).get()
println( "$a is $c $b." )</
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Integer_comparison}}
'''Solution'''
Fōrmulæ has an intrinsic expression for [[wp:Three-way_comparison|three-way comparison]] that works for integers and other types (decimal and rational numbers, strings, time expressions, etc). It returns expressions that flag one of the three possible results.
[[File:Fōrmulæ - Integer comparison 01.png]]
[[File:Fōrmulæ - Integer comparison 02.png]]
However, a simple function can be written for the requirement:
[[File:Fōrmulæ - Integer comparison 03.png]]
[[File:Fōrmulæ - Integer comparison 04.png]]
[[File:Fōrmulæ - Integer comparison 05.png]]
=={{header|Go}}==
<
import (
Line 1,758 ⟶ 2,064:
fmt.Println(n1, "greater than", n2)
}
}</
=={{header|Groovy}}==
===Relational Operators===
<
println "a ? b = ${a} ? ${b} = a ${a < b ? '<' : a > b ? '>' : a == b ? '==' : '?'} b"
}</
Program:
<
comparison(2000,300000)
comparison(2000,2000)</
{{out}}
Line 1,779 ⟶ 2,085:
==="Spaceship" (compareTo) Operator===
Using spaceship operator and a lookup table:
<
def comparisonSpaceship = { a, b ->
println "a ? b = ${a} ? ${b} = a ${rels[a <=> b]} b"
}</
Program:
<
comparison(2000,300000)
comparison(2000,2000)</
{{out}}
Line 1,795 ⟶ 2,101:
=={{header|Harbour}}==
<
IF a < b
Line 1,805 ⟶ 2,111:
ENDIF
RETURN</
=={{header|Haskell}}==
<
myCompare a b
| a < b = "A is less than B"
Line 1,817 ⟶ 2,123:
a <- readLn
b <- readLn
putStrLn $ myCompare a b</
However, the more idiomatic and less error-prone way to do it in Haskell would be to use a compare function that returns type Ordering, which is either LT, GT, or EQ:
<
LT -> "A is less than B"
GT -> "A is greater than B"
EQ -> "A equals B"</
=={{header|hexiscript}}==
<
let b scan int
Line 1,838 ⟶ 2,144:
if a = b
println a + " is equal to " + b
endif</
=={{header|HicEst}}==
<
IF (a < b) THEN
Line 1,849 ⟶ 2,155:
ELSEIF(a > b) THEN
WRITE(Messagebox) a, ' is greater than ', b
ENDIF</
=={{header|HolyC}}==
<
a = Str2I64(GetStr("Enter your first number: "));
b = Str2I64(GetStr("Enter your second number: "));
Line 1,863 ⟶ 2,169:
if (a > b)
Print("%d is greater than %d\n", a, b);</
=={{header|Hy}}==
<
(def b (int (input "Enter value of b: ")))
(print (cond [(< a b) "a is less than b"]
[(> a b) "a is greater than b"]
[(= a b) "a is equal to b"]))</
=={{header|i}}==
<
a = number(read(' '))
b = number(read())
Line 1,889 ⟶ 2,195:
print(a, " is greater than ", b)
end
}</
=={{header|Icon}} and {{header|Unicon}}==
<
until integer(a) do {
Line 1,907 ⟶ 2,213:
write(a," = ", a = b)
write(a," > ", a > b)
end</
{{out}}
Line 1,917 ⟶ 2,223:
=={{header|J}}==
Comparison is accomplished by the verb <code>compare</code>, which provides logical-numeric output.<br>Text elaborating the output of <code>compare</code> is provided by <code>cti</code>:
<
cti=: dyad define
Line 1,923 ⟶ 2,229:
English =. ' is less than ';' is equal to ';' is greater than '
x (":@[, (compare select English"_), ":@]) y
)</
Examples of use:
<
0 1 0
4 cti 3
4 is greater than 3</
=={{header|Java}}==
<
public class compInt {
Line 1,951 ⟶ 2,257:
} catch(IOException e) { }
}
}</
=={{header|JavaScript}}==
<
// Using type coercion
function compare(a, b) {
Line 1,981 ⟶ 2,287:
}
}
</syntaxhighlight>
=={{header|Joy}}==
<
DEFINE
prompt == "Please enter a number and <Enter>: " putchars;
Line 1,990 ⟶ 2,296:
putln == put newline.
stdin # F
prompt fgets # S F
10 strtol # A F
swap # F A
dupd # F A A
prompt fgets # S F A A
10 strtol # B F A A
popd # B A A
dup # B B A A
rollup # B A B A
[<] [swap put "is less than " putchars putln] [] ifte
[=] [swap put "is equal to " putchars putln] [] ifte
[>] [swap put "is greater than " putchars putln] [] ifte
# B A
quit.</
=={{header|jq}}==
<
# otherwise an "uncomparable" message is emitted.
Line 2,022 ⟶ 2,328:
end ;
compare</
Examples;
<syntaxhighlight lang="jq">
$ jq -s -r -f Integer_comparison.jq
1 2
Line 2,033 ⟶ 2,339:
1 "a"
1 is uncomparable to a
</syntaxhighlight>
=={{header|Julia}}==
<
int1 = readline(stdin)
int2 = readline(stdin)
Line 2,046 ⟶ 2,352:
int2)
end
</
<pre>julia> compare()
3
Line 2,054 ⟶ 2,360:
=={{header|Kotlin}}==
<syntaxhighlight lang
val n1 = readLine()!!.toLong()
val n2 = readLine()!!.toLong()
Line 2,063 ⟶ 2,369:
else -> ""
})
}</
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def compare
{lambda {:a :b}
Line 2,083 ⟶ 2,389:
{compare 1 1}
-> 1 is equal to 1
</syntaxhighlight>
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fn.print(Enter a:\s)
$a = fn.int(fn.input())
fn.print(Enter b:\s)
$b = fn.int(fn.input())
if($a < $b) {
fn.println($a is less than $b)
}elif($a > $b) {
fn.println($a is greater than $b)
}elif($a === $b) {
fn.println($a is equals to $b)
}
</syntaxhighlight>
=={{header|Lasso}}==
<
number1 = integer(web_request -> param('number1')),
number2 = integer(web_request -> param('number2'))
Line 2,095 ⟶ 2,418:
#number1 == #number2 ? 'Number 1 is the same as Number 2' | 'Number 1 is not the same as Number 2'
'<br />'
#number1 > #number2 ? 'Number 1 is greater than Number 2' | 'Number 1 is not greater than Number 2'</
{{out}}
Line 2,102 ⟶ 2,425:
Number 1 is the same as Number 2
Number 1 is not greater than Number 2</pre>
=={{header|LIL}}==
<
set rc [readline]
set a [index $rc 0]
Line 2,144 ⟶ 2,434:
if {$a < $b} {print "$a is less than $b"}
if {$a == $b} {print "$a is equal to $b"}
if {$a > $b} {print "$a is greater than $b"}</
=={{header|Lingo}}==
Lingo programs are normally not run in the console, so interactive user input is handled via GUI. To not clutter this page with GUI creation code, here only the comparison part of the task:
<
if a < b then put a&" is less than "&b
if a = b then put a&" is equal to "&b
if a > b then put a&" is greater than "&b
end</
=={{header|LiveCode}}==
<
if it is not empty then
put item 1 of it into num1
Line 2,174 ⟶ 2,464:
end switch
end if
end if</
=={{header|LLVM}}==
Line 2,181 ⟶ 2,471:
{{libheader|cstdlib}}
<
;e means little endian
;p: { pointer size : pointer abi : preferred alignment for pointers }
Line 2,272 ⟶ 2,562:
declare i32 @printf(i8* nocapture, ...) nounwind
</syntaxhighlight>
=={{header|Logo}}==
<
if :a = :b [(print :a [equals] :b)]
if :a < :b [(print :a [is less than] :b)]
if :a > :b [(print :a [is greater than] :b)]
end</
Each infix operator has prefix synonyms (equalp, equal?, lessp, less?, greaterp, greater?), where the 'p' stands for "predicate" as in [[Lisp]].
Line 2,286 ⟶ 2,576:
==={{header|avec SI}}===
<
ENTIER A, B
LIRE ['Entrez un premier entier:',U] A
Line 2,298 ⟶ 2,588:
SI A > B ALORS
AFFICHER [U,' est plus grand que ',U,/] A, B
FIN SI</
==={{header|avec ÉVALUER}}===
<
ENTIER A, B
LIRE ['Entrez un premier entier:',U] A
Line 2,312 ⟶ 2,602:
QUAND AUTRE
AFFICHER [U,' est plus grand que ',U,/] A, B
FIN EVALUER</
==={{header|avec SI..IS}}===
<
ENTIER A, B
LIRE ['Entrez un premier entier:',U] A
Line 2,323 ⟶ 2,613:
SI A = B ALORS ' est égale à ' SINON \
SI A > B ALORS ' est plus grand que ' SINON '?' IS IS IS, B
</syntaxhighlight>
=={{header|LSE64}}==
<
2dup : over over
Line 2,333 ⟶ 2,623:
compare : 2dup > then " is more than"
show : compare rot , sp ,t sp , nl</
=={{header|Lua}}==
<
a = tonumber(io.stdin:read())
print('Enter the second number: ')
Line 2,343 ⟶ 2,633:
if a < b then print(a .. " is less than " .. b) end
if a > b then print(a .. " is greater than " .. b) end
if a == b then print(a .. " is equal to " .. b) end</
In lua, a double equals symbol is required to test for equality. A single equals sign is used for assignment, and will cause an error during jit precompilation, if it is used within a boolean expression:
<
=={{header|Maple}}==
<
local a, b;
printf( "Enter a number:> " );
Line 2,365 ⟶ 2,655:
end proc:
CompareNumbers();</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
b=Input["Give me the value for b please!"];
If[a==b,Print["a equals b"]]
If[a>b,Print["a is bigger than b"]]
If[a<b,Print["b is bigger than a"]]</
=={{header|Maxima}}==
<
block(
[a: read("a?"), b: read("b?")],
Line 2,383 ⟶ 2,673:
if a >= b then print(a, ">=", b),
if a = b then print(a, "=", b),
if a # b then print(a, "#", b))$</
=={{header|MAXScript}}==
<
b = getKBValue prompt:"Enter value of b:"
if a < b then print "a is less then b"
else if a > b then print "a is greater then b"
else if a == b then print "a is equal to b"</
=={{header|Metafont}}==
<
a1 := scantokens readstring;
message "integer 2: ";
Line 2,404 ⟶ 2,694:
message decimal a1 & " is equal to " & decimal a2
fi;
end</
=={{header|min}}==
{{works with|min|0.19.3}}
<
("Enter an integer" ask) 2 times over over
(
Line 2,415 ⟶ 2,705:
((==) ("equal to"))
) case
' append prepend % print</
=={{header|MiniScript}}==
<
integer2 = input("Please Input Integer 2:").val
if integer1 < integer2 then print integer1 + " is less than " + integer2
if integer1 == integer2 then print integer1 + " is equal to " + integer2
if integer1 > integer2 then print integer1 + " is greater than " + integer2</
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">- ЗН С/П</
''Input:'' a ^ b
Line 2,435 ⟶ 2,725:
Note that ML/I only has tests for ''equality'', ''greater-than'', and ''greater-than-or-equal''.
<
"" assumes macros on input stream 1, terminal on stream 2
MCSKIP MT,<>
Line 2,452 ⟶ 2,742:
>
MCSET S1=1
~MCSET S10=2</
=={{header|MMIX}}==
Some simple error checking is included.
<
p IS $255 % pointer
pp GREG % backup for p
Line 2,540 ⟶ 2,830:
LDA p,GT % _ : print 'GT'
2H TRAP 0,Fputs,StdOut % print result
TRAP 0,Halt,0</
Example of use:
<pre>~/MIX/MMIX/Progs> mmix compare2ints 121 122
Line 2,561 ⟶ 2,851:
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 2,582 ⟶ 2,872:
InOut.WriteInt(B, 1);
InOut.WriteLn
END IntCompare.</
=={{header|Modula-3}}==
<
FROM IO IMPORT Put, GetInt;
Line 2,602 ⟶ 2,892:
Put(Int(a) & " is greater than " & Int(b) & "\n");
END;
END Main.</
=={{header|MUMPS}}==
<syntaxhighlight lang="text">INTCOMP
NEW A,B
INTCOMPREAD
Line 2,615 ⟶ 2,905:
IF A>B WRITE !,A," is greater than ",B
KILL A,B
QUIT</
{{out}}
<pre>USER>d INTCOMP^ROSETTA
Line 2,634 ⟶ 2,924:
=={{header|Nanoquery}}==
<
first = int(input())
print "enter second integer: "
Line 2,645 ⟶ 2,935:
else if first > second
println first + " is greater than " + second
end</
=={{header|Nemerle}}==
Showing both the use of comparison operators and the .Net Int32.CompareTo() method.
<
using System.Console;
Line 2,679 ⟶ 2,969:
}
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 2,697 ⟶ 2,987:
return
</syntaxhighlight>
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(print "Please enter the first number: ")
(set 'A (int (read-line)))
Line 2,712 ⟶ 3,002:
(true "less than"))
" the second.")
</syntaxhighlight>
=={{header|Nim}}==
<
var a = parseInt(readLineFromStdin "Enter value of a: ")
var b = parseInt(readLineFromStdin "Enter value of b: ")
Line 2,724 ⟶ 3,014:
echo "a is greater than b"
elif a == b:
echo "a is equal to b"</
=={{header|NSIS}}==
===Pure NSIS (Using [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.4.13 IntCmp] directly)===
<
Function IntergerComparison
Push $0
Line 2,751 ⟶ 3,041:
Pop $0
FunctionEnd
</syntaxhighlight>
=== Using [http://nsis.sourceforge.net/LogicLib LogicLib] (bundled library) ===
{{libheader|LogicLib}}
<
Function IntegerComparison
Push $0
Line 2,773 ⟶ 3,063:
Pop $0
FunctionEnd
</syntaxhighlight>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
[First Second] | each {input $"($in) integer: "| into int} | [
[($in.0 < $in.1) "less than"]
[($in.0 == $in.1) "equal to"]
[($in.0 > $in.1) "greater than"]
] | each {if $in.0 {$"The first integer is ($in.1) the second integer."}} | str join "\n"
</syntaxhighlight>
=={{header|Oberon-2}}==
<
IMPORT In, Out;
Line 2,801 ⟶ 3,100:
Out.Ln;
END;
END Compare.</
=={{header|Objeck}}==
<
bundle Default {
class IntCompare {
Line 2,825 ⟶ 3,124:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
if a < b then "A is less than B"
else if a > b then "A is greater than B"
Line 2,837 ⟶ 3,136:
let a = read_int ()
and b = read_int () in
print_endline (my_compare a b)</
=={{header|Octave}}==
<
a = scanf("%d", "C");
printf("Enter b: ");
Line 2,850 ⟶ 3,149:
elseif (a < b)
disp("a less than b");
endif</
=={{header|Oforth}}==
<
: cmpInt
Line 2,863 ⟶ 3,162:
a b < ifTrue: [ System.Out a << " is less than " << b << cr ]
a b == ifTrue: [ System.Out a << " is equal to " << b << cr ]
a b > ifTrue: [ System.Out a << " is greater than " << b << cr ] ;</
=={{header|Ol}}==
<
(define (compare a b)
(cond ((< a b) "A is less than B")
Line 2,883 ⟶ 3,182:
; manual user input:
(print (compare (read) (read)))
</syntaxhighlight>
=={{header|Oz}}==
<
import
Application(exit)
Line 2,913 ⟶ 3,212:
{Application.exit 0}
end</
=={{header|PARI/GP}}==
<
b=input();
if(a<b, print(a" < "b));
if(a==b, print(a" = "b));
if(a>b, print(a" > "b));</
=={{header|Pascal}}==
<
var
Line 2,936 ⟶ 3,235:
if (a = b) then writeln(a, ' is equal to ', b);
if (a > b) then writeln(a, ' is greater than ', b);
end.</
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var (a,b) := ReadInteger2('Enter a,b:');
if a < b then
Println($'{a} is less then {b}')
else if a > b then
Println($'{a} is greater then {b}')
else Println($'{a} is equal to then {b}')
end.
</syntaxhighlight>
=={{header|Perl}}==
Line 2,943 ⟶ 3,255:
Separate tests for less than, greater than, and equals
<
my $f = shift;
my $s = shift;
Line 2,955 ⟶ 3,267:
return 0; # returns 0 $f is equal to $s
};
};</
All three tests in one. If $f is less than $s return -1, greater than return 1, equal to return 0
<
return $_[0] <=> $_[1];
};</
Note: In Perl, $a and $b are (kind of) reserved identifiers for the built-in ''sort'' function. It's good style to use more meaningful names, anyway.
<
print "Enter two integers: ";
($x, $y) = split ' ', <>;
print $x, (" is less than ", " is equal to ",
" is greater than ")[test_num($x, $y) + 1], $y, "\n";</
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<
<span style="color: #004080;">atom</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prompt_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"first number:"</span><span style="color: #0000FF;">,{}),</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prompt_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"second number:"</span><span style="color: #0000FF;">,{})</span>
Line 2,985 ⟶ 3,297:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %g"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<!--</
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Integer_comparison
by Galileo, 10/2022 #/
"Enter first number: " input tonum nl
"Enter second number: " input tonum nl
over over -
rot print " is " print
/# dup 0 < if drop "less" else 0 > if "greater" else "equal" endif endif #/
dup 0 < if drop "less" else dup 0 > if drop "greater" else dup 0 == if drop "equal" endif endif endif
print " than " print print
</syntaxhighlight>
{{out}}
<pre>Enter first number: 5
Enter second number: 1
5 is greater than 1
=== Press any key to exit ===</pre>
=={{header|PHL}}==
<
extern printf;
Line 3,009 ⟶ 3,340:
return 0;
]</
=={{header|PHP}}==
<
echo "Enter an integer [int1]: ";
Line 3,038 ⟶ 3,369:
echo "int1 > int2\n";
?></
Note that this works from the command-line interface only, whereas [http://www.php.net PHP] is usually executed as [[wp:Common_Gateway_Interface CGI]].
=={{header|PicoLisp}}==
<
(in NIL # Read from standard input
Line 3,052 ⟶ 3,383:
((= A B) "equal to")
(T "less than") )
" the second." ) ) )</
{{out}}
<pre>Please enter two values: 4 3
Line 3,058 ⟶ 3,389:
=={{header|Pike}}==
<
if(argc != 3){
write("usage: `pike compare-two-ints.pike <x> <y>` where x and y are integers.\n");
Line 3,074 ⟶ 3,405:
write(a + " is equal to " + b + "\n");
}
}</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare (a, b) fixed binary;
Line 3,087 ⟶ 3,418:
if a < b then
put skip list ('The second number is greater than the first');
</syntaxhighlight>
=={{header|Plain English}}==
<
Start up.
Write "Enter the first number: " to the console without advancing.
Line 3,100 ⟶ 3,431:
If the first number is greater than the second number, write "Greater than!" to the console.
Wait for the escape key.
Shut down.</
=={{header|Pop11}}==
<
define compare_integers(x, y);
if x > y then
Line 3,119 ⟶ 3,450:
;;; Read numbers and call comparison procedure
compare_integers(itemrep(), itemrep());</
=={{header|PowerShell}}==
<
$b = [int] (Read-Host b)
Line 3,131 ⟶ 3,462:
} elseif ($a -gt $b) {
Write-Host $a is greater than $b`.
}</
=={{header|Python}}==
<
a = input('Enter value of a: ')
b = input('Enter value of b: ')
Line 3,164 ⟶ 3,474:
print 'a is greater than b'
elif a == b:
print 'a is equal to b'</
(Note: in Python3 ''input()'' will become ''int(input())'')
Line 3,171 ⟶ 3,481:
{{works with|Python|2.x only, not 3.x}}
<
import sys
try:
Line 3,185 ⟶ 3,495:
1: 'is greater than'
}
print a, dispatch[cmp(a,b)], b</
In this case the use of a dispatch table is silly. However, more generally in Python the use of dispatch dictionaries or tables is often preferable to long chains of '''''elif'''' clauses in a condition statement. Python's support of classes and functions (including [[currying]], partial function support, and lambda expressions) as first class objects obviates the need for a "case" or "switch" statement.
Line 3,191 ⟶ 3,501:
=={{header|Quackery}}==
<
input quackery cr
say "The first number is "
Line 3,198 ⟶ 3,508:
2dup < if [ say "smaller than" ] ]
2drop
say " the second number." cr</
{{out}}
Line 3,220 ⟶ 3,530:
=={{header|R}}==
<
a <- scan(what=numeric(0), nmax=1)
print("insert number b")
Line 3,230 ⟶ 3,540:
} else if ( a == b ) { # could be simply else of course...
print("a and b are the same")
}</
=={{header|Racket}}==
<
(define (compare-two-ints a b)
(define compared
Line 3,241 ⟶ 3,551:
(format "~a ~a ~a" a compared b))
(compare-two-ints (read) (read))</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my $b = prompt("2nd int: ").floor;
Line 3,256 ⟶ 3,566:
elsif $a == $b {
say 'Equal';
}</
With <code><=></code>:
<syntaxhighlight lang="raku"
A three-way comparison such as <tt><=></tt> actually returns an <code>Order</code> enum which stringifies into 'Decrease', 'Increase' or 'Same'. So if it's ok to use this particular vocabulary, you could say that this task is actually a built in:
<syntaxhighlight lang="raku"
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">output: "Enter two integers, a and b"
input: a
input: b
case
when a > b:
output: "a is greater than b"
when a < b:
output: "a is less than b"
when a = b:
output: "a is equal to b"
esac</syntaxhighlight>
=={{header|Raven}}==
<
expect trim 1.1 prefer as $a
"Enter the second number: " print
Line 3,274 ⟶ 3,598:
$a $b < if $b $a "%g is less than %g\n" print
$a $b > if $b $a "%g is greater than %g\n" print
$a $b = if $b $a "%g is equal to %g\n" print</
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
REBOL [
Title: "Comparing Two Integers"
Line 3,291 ⟶ 3,615:
]
print [a "is" case relation b]
</syntaxhighlight>
==={{header|Relation}}===
There is no input, so numbers have to be given in code
<syntaxhighlight lang="relation">
set a = 15
set b = 21
Line 3,307 ⟶ 3,631:
end if
end if
</syntaxhighlight>
=={{header|Red}}==
{{trans|REBOL}}
<
Title: "Comparing Two Integers"
URL: http://rosettacode.org/wiki/Comparing_two_integers
Line 3,324 ⟶ 3,648:
a > b "greater than"
]
print [a "is" case relation b]</
=={{header|ReScript}}==
<
if a < b { "A is less than B" } else
if a > b { "A is greater than B" } else
Line 3,338 ⟶ 3,662:
let b = int_of_string(Sys.argv[3])
Js.log(my_compare(a, b))</
{{out}}
<pre>
Line 3,353 ⟶ 3,677:
Taking the numbers from the stack:
<
dup-pair gt? [ 'A>B s:put nl ] if
dup-pair lt? [ 'A<B s:put nl ] if
eq? [ 'A=B s:put nl ] if ;</
=={{header|REXX}}==
<
numeric digits 2000 /*for the users that really go ka─razy.*/
@=copies('─', 20) /*eyeball catcher for the user's eyen. */
Line 3,382 ⟶ 3,706:
end /*forever*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
serr: say @ '***error*** ' arg(1); say @ "Please try again."; return</
{{out|output|text= (shows user input and computer program output together):}}
<pre>
Line 3,410 ⟶ 3,734:
=={{header|Ring}}==
<
Func Compare a,b
if a < b
Line 3,419 ⟶ 3,743:
See "A equals B"
ok
</syntaxhighlight>
=={{header|Rockstar}}==
Line 3,425 ⟶ 3,749:
Minimized Rockstar:
<syntaxhighlight lang="rockstar">
(Get two numbers from user)
Listen to Number One
Line 3,440 ⟶ 3,764:
If Number One is less than Number Two
Say "The first is less than the second"
</syntaxhighlight>
Idiomatic version:
<syntaxhighlight lang="rockstar">
Listen to your soul
Listen to my words
Line 3,455 ⟶ 3,779:
If your soul is smaller than my words,
Say "The second was bigger".
</syntaxhighlight>
=={{header|RPG}}==
Line 3,462 ⟶ 3,786:
CALL rc_intcmp (x'00000000' x'00000001')
<syntaxhighlight lang="rpg">
h dftactgrp(*no)
Line 3,485 ⟶ 3,809:
dsply message;
*inlr = *on;
</syntaxhighlight>
=={{header|RPL}}==
≪ '''IF''' DUP2 < '''THEN''' "a < b" ROT ROT '''END'''
'''IF''' DUP2 == '''THEN''' "a = b" ROT ROT '''END'''
'''IF''' > '''THEN''' "a > b" '''END'''
≫ 'COMPR' STO
{{in}}
<pre>
3 5 COMPR
</pre>
{{out}}
<pre>
1: "a < b"
</pre>
=={{header|Ruby}}==
This uses Kernel#gets to get input from STDIN, and String#to_i to convert the string into an integer. (Without this conversion, Ruby would compare strings: 5 < 10 but "5" > "10".)
<
b = (print "enter a value for b: "; gets).to_i
puts "#{a} is less than #{b}" if a < b
puts "#{a} is greater than #{b}" if a > b
puts "#{a} is equal to #{b}" if a == b</
Another way:
<
b = (print "enter a value for b: "; gets).to_i
Line 3,506 ⟶ 3,844:
when 0; puts "#{a} is equal to #{b}"
when +1; puts "#{a} is greater than #{b}"
end</
Example '''input''' and output:
Line 3,524 ⟶ 3,862:
An alternative method, which is similar to the python version mentioned above (at the time of writing this) is:
<
def prompt str
print str, ": "
Line 3,547 ⟶ 3,885:
# I hope you can figure this out
puts "#{a} is #{dispatch[a<=>b]} #{b}"</
=={{header|Rust}}==
Reading from stdin into a string is cumbersome at the moment, but convenience functions will be implemented in the future.
<
fn main() {
Line 3,577 ⟶ 3,907:
println!("{} is greater than {}" , a , b)
};
}</
=={{header|SAS}}==
<
data _null_;
input a b;
Line 3,602 ⟶ 3,932:
1 1
;
run;</
=={{header|Scala}}==
<
def main(args: Array[String]): Unit = {
val a=Console.readInt
Line 3,616 ⟶ 3,946:
printf("%d is greater than %d\n", a, b)
}
}</
=={{header|Scheme}}==
<
(cond ((< a b) "A is less than B")
((> a b) "A is greater than B")
((= a b) "A equals B")))
(my-compare (read) (read))</
=={{header|Seed7}}==
<
const proc: main is func
Line 3,648 ⟶ 3,978:
writeln(a <& " is greater than " <& b);
end if;
end func;</
=={{header|SenseTalk}}==
<
Put It into A
Line 3,670 ⟶ 4,000:
If A is Equal to B
Put A& " is Equal to " &B
End If</
=={{header|Sidef}}==
<
var b = read("b: ", Number);
Line 3,684 ⟶ 4,014:
elsif (a > b) {
say 'Greater';
}</
{{out}}
<pre>
Line 3,694 ⟶ 4,024:
=={{header|Slate}}==
<
( a > b ) ifTrue: [ inform: 'a greater than b\n' ].
Line 3,700 ⟶ 4,030:
( a = b ) ifTrue: [ inform: 'a is equal to b\n' ].
] applyTo: {Integer readFrom: (query: 'Enter a: '). Integer readFrom: (query: 'Enter b: ')}.</
=={{header|Smalltalk}}==
<
'a = ' display. a := (stdin nextLine asInteger).
'b = ' display. b := (stdin nextLine asInteger).
( a > b ) ifTrue: [ 'a greater than b' displayNl ].
( a < b ) ifTrue: [ 'a less than b' displayNl ].
( a = b ) ifTrue: [ 'a is equal to b' displayNl ].</
=={{header|SNOBOL4}}==
Line 3,715 ⟶ 4,045:
Comparisons in Snobol are not operators, but predicate functions that return a null string and generate a success or failure value which allows or blocks statement execution, and which can be tested for branching. Other numeric comparisons are ge (>=), le (<=) and ne (!= ). There is also a parallel set of L-prefixed predicates in modern Snobols for lexical string comparison.
<
output = 'Enter X,Y:'
trim(input) break(',') . x ',' rem . y
Line 3,722 ⟶ 4,052:
output = eq(x,y) x ' is equal to ' y :s(end)
output = gt(x,y) x ' is greater than ' y
end</
=={{header|SNUSP}}==
There are no built-in comparison operators, but you can (destructively) check which of two adjacent cells is most positive.
<
> - \# a>b
- <
a<b #\?/</
=={{header|Sparkling}}==
<
if a < b {
print("a < b");
Line 3,741 ⟶ 4,071:
} else {
print("either a or b or both are NaN");
}</
=={{header|SQL}}==
{{works with|Oracle}}
<
drop table test;
Line 3,767 ⟶ 4,097:
from test
where a > b;
</syntaxhighlight>
<pre>
Line 3,789 ⟶ 4,119:
{{works with|Db2 LUW}}
With SQL only:
<
CREATE TABLE TEST (
VAL1 INT,
Line 3,805 ⟶ 4,135:
END COMPARISON
FROM TEST;
</syntaxhighlight>
Output:
<pre>
Line 3,837 ⟶ 4,167:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 3,855 ⟶ 4,185:
CALL COMPARISON(2, 2) @
CALL COMPARISON(2, 1) @
</syntaxhighlight>
Output:
<pre>
Line 3,907 ⟶ 4,237:
halt</pre>
To run the SSEM program, load A into storage address 21 and B into storage address 22. No additional space is used. Like the pseudocode version, the program halts with the accumulator holding 1 if A>B, 0 if A=B, or -1 if A<B.
<
10101000000001100000000000000000 1. c to 21
10101000000000100000000000000000 2. -21 to c
Line 3,927 ⟶ 4,257:
11111111111111111111111111111111 18. -1
11010000000000000000000000000000 19. 11
10110000000000000000000000000000 20. 13</
=={{header|Standard ML}}==
<
if a < b then print "A is less than B\n"
if a > b then print "A is greater than B\n"
Line 3,943 ⟶ 4,273:
compare_integers (a, b)
end
handle Bind => print "Invalid number entered!\n"</
A more idiomatic and less error-prone way to do it in SML would be to use a compare function that returns type <tt>order</tt>, which is either LESS, GREATER, or EQUAL:
<
LESS => "A is less than B"
| GREATER => "A is greater than B"
| EQUAL => "A equals B"</
=={{header|Swift}}==
<
var input = NSFileHandle.fileHandleWithStandardInput()
Line 3,967 ⟶ 4,297:
if (a==b) {println("\(a) equals \(b)")}
if (a < b) {println("\(a) is less than \(b)")}
if (a > b) {println("\(a) is greater than \(b)")}</
{{out}}
<pre>
Line 3,978 ⟶ 4,308:
This is not how one would write this in Tcl, but for the sake of clarity:
<
gets stdin x
Line 3,985 ⟶ 4,315:
if { $x > $y } { puts "$x is greater than $y" }
if { $x < $y } { puts "$x is less than $y" }
if { $x == $y } { puts "$x equals $y" }</
Other comparison operators are "<=", ">=" and "!=".
Line 3,991 ⟶ 4,321:
Note that Tcl doesn't really have a notion of a variable "type" - all variables are just strings of bytes and notions like "integer" only ever enter at interpretation time. Thus the above code will work correctly for "5" and "6", but "5" and "5.5" will also be compared correctly. It will not be an error to enter "hello" for one of the numbers ("hello" is greater than any integer). If this is a problem, the type can be expressly cast
<
or otherwise [[IsNumeric | type can be checked]] with "<tt>if { string is integer $x }...</tt>"
Line 3,998 ⟶ 4,328:
A variant that iterates over comparison operators, demonstrated in an interactive [[tclsh]]:
<
% foreach {o s} {< "less than" > "greater than" == equal} {if [list $i $o $j] {puts "$i is $s $j"}}
5 is less than 6
Line 4,006 ⟶ 4,336:
% set j 4
% foreach {o s} {< "less than" > "greater than" == equal} {if [list $i $o $j] {puts "$i is $s $j"}}
5 is greater than 4</
=={{header|Toka}}==
<
2dup < [ ." a is less than b\n" ] ifTrue
2dup > [ ." a is greater than b\n" ] ifTrue
Line 4,038 ⟶ 4,347:
1 1 compare-integers
2 1 compare-integers
1 2 compare-integers</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
Line 4,052 ⟶ 4,361:
IF (i1<i2) PRINT i1," is less than ",i2
IF (i1>i2) PRINT i1," is greater than ",i2
</syntaxhighlight>
=={{header|UNIX Shell}}==
Line 4,059 ⟶ 4,368:
{{works with|ksh93}}
<
# tested with ksh93s+
Line 4,080 ⟶ 4,389:
fi
exit 0</
One can backport the previous code to pdksh, which has no builtin printf, but can call /usr/bin/printf as an external program.
Line 4,086 ⟶ 4,395:
{{works with|pdksh}}
<
# tested with pdksh
Line 4,105 ⟶ 4,414:
fi
exit 0</
----
Line 4,111 ⟶ 4,420:
{{works with|Bash}}
<
if [ $a -gt $b ]; then comparison="greater than"
Line 4,119 ⟶ 4,428:
fi
echo "${a} is ${comparison} ${b}"</
=={{header|Ursa}}==
<
out "enter first integer: " console
set first (in int console)
Line 4,136 ⟶ 4,445:
if (> first second)
out first " is greater than " second endl console
end if</
=={{header|V}}==
<
[ [>] ['less than' puts]
[<] ['greater than' puts]
Line 4,150 ⟶ 4,459:
less than
|2 2 compare
is equal</
=={{header|Vala}}==
<
void main(){
int a;
Line 4,171 ⟶ 4,480:
stdout.printf("%d is greater than %d\n", a, b);
}
</syntaxhighlight>
=={{header|Wart}}==
<
b <- (read)
prn (if (a < b)
Line 4,249 ⟶ 4,490:
: "a is greater than b"
:else
: "a equals b")</
=={{header|Wren}}==
<
System.print("Enter anything other than a number to quit at any time.\n")
Line 4,273 ⟶ 4,514:
}
System.print()
}</
{{out}}
Line 4,296 ⟶ 4,537:
=={{header|XLISP}}==
<
(DISPLAY "Enter two integers separated by a space.")
(NEWLINE)
Line 4,305 ⟶ 4,546:
((> A B) (DISPLAY "The first number is larger."))
((= A B) (DISPLAY "They are equal."))
((< A B) (DISPLAY "The first number is smaller."))))</
=={{header|XPL0}}==
<
int A, B;
[A:= IntIn(0);
Line 4,316 ⟶ 4,557:
if A>B then Text(0, "A>B");
CrLf(0);
]</
=={{header|XSLT}}==
Because XSLT uses XML syntax, the less than and greater than operators which would normally be written '<' and '>' must be escaped using character entities, even inside of XPath expressions.
<
<xsl:param name="a" select="1"/>
<xsl:param name="b" select="2"/>
Line 4,331 ⟶ 4,572:
</xsl:choose>
</fo:block>
</xsl:template></
=={{header|zkl}}==
<
(if (x==y) "equal" else if (x<y) "less" else if(x>y) "greater").println()</
{{out}}
<pre>
Line 4,341 ⟶ 4,582:
greater
</pre>
|