Integer comparison: Difference between revisions
Content deleted Content added
Add SmallBASIC |
|||
(18 intermediate revisions by 13 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}}==
Line 71:
Specific OS/hardware routines for user input and printing are left unimplemented.
It should be noted that this is strictly an unsigned comparison.
<
JSR GetUserInput ;routine not implemented
;integers to compare now in memory locations A and B
Line 85:
A_equals_B: JSR DisplayAEqualsB ;routine not implemented
Done: PLA ;restore Accumulator from stack
RTS ;return from subroutine</
=={{header|68000 Assembly}}==
Line 93:
===Unsigned Comparison===
<
;integers to compare are in D0 and D1
CMP.L D1,D0
Line 118:
Equal:
dc.b "both are equal",0
even</
===Signed Comparison===
<
;integers to compare are in D0 and D1
CMP.L D1,D0
Line 146:
Equal:
dc.b "both are equal",0
even</
=={{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 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 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 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 265:
write comparison_result.
</syntaxhighlight>
=={{header|Action!}}==
<
INT a,b
Line 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 293:
=={{header|Ada}}==
<
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
Line 316:
Put_Line("A is greater than B");
end if;
end Compare_Ints;</
=={{header|Aime}}==
<
output(integer a, integer b, text condition)
{
Line 345:
return 0;
}</
Run as:
<pre>aime FILE integer a 33 integer b 133</pre>
Line 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 383:
print((a," is greater or equal to ", b, new line))
FI
)</
{{out}}
<pre>
Line 392:
=={{header|ALGOL W}}==
<
integer a, b;
Line 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 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 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 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 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 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 711 ⟶ 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 729 ⟶ 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 742 ⟶ 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 768 ⟶ 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|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
PRINT "Please enter two integers, separated by a comma: ";
INPUT x, y
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
</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 784 ⟶ 986:
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 813 ⟶ 1,076:
) else ( if %a% EQU %b% (
echo %a% is equal to %b%
)))</
{{Out}}
<pre>C:\Test>IntegerComparison.bat
Line 819 ⟶ 1,082:
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 842 ⟶ 1,113:
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 858 ⟶ 1,129:
"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 885 ⟶ 1,156:
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 905 ⟶ 1,176:
return 0;
}</
=={{header|C sharp|C#}}==
<
class Program
Line 923 ⟶ 1,194:
Console.WriteLine("{0} is greater than {1}", a, b);
}
}</
=={{header|C++}}==
<
int main()
Line 948 ⟶ 1,219:
if (a > b)
std::cout << a << " is greater than " << b << "\n";
}</
=={{header|CFEngine}}==
<
bundle agent __main__
{
Line 967 ⟶ 1,238:
if => isgreaterthan( "$(first_integer)", "$(second_integer)" );
}
</syntaxhighlight>
=={{header|ChucK}}==
<syntaxhighlight lang="text">
fun void intComparison (int one, int two)
{
Line 979 ⟶ 1,250:
// uncomment next line and change values to test
// intComparison (2,4);
</syntaxhighlight>
=={{header|Clean}}==
<
compare a b
Line 993 ⟶ 1,264:
(_, a, console) = freadi console
(_, b, console) = freadi console
= compare a b</
=={{header|Clipper}}==
<
IF a < b
? "A is less than B"
Line 1,005 ⟶ 1,276:
ENDIF
Return Nil
</syntaxhighlight>
=={{header|Clojure}}==
Line 1,011 ⟶ 1,282:
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 1,037 ⟶ 1,308:
if(A GREATER B)
message(STATUS "${A} is greater than ${B}")
endif()</
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
PROGRAM-ID. Int-Compare.
Line 1,066 ⟶ 1,337:
GOBACK
.</
=={{header|ColdFusion}}==
Line 1,079 ⟶ 1,350:
===In CFML===
<
<cfargument name="Integer1" type="numeric">
<cfargument name="Integer2" type="numeric">
Line 1,102 ⟶ 1,373:
</cfif>
<cfreturn VARIABLES.Result >
</cffunction></
===In CFScript===
<
function CompareInteger( Integer1, Integer2 ) {
VARIABLES.Result = "";
Line 1,129 ⟶ 1,400:
return VARIABLES.Result;
}
</cfscript></
=={{header|Common Lisp}}==
You can type this directly into a REPL:
<
(b (read *standard-input*)))
(cond
Line 1,142 ⟶ 1,413:
((> 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,148 ⟶ 1,419:
Alternatively, you can wrap this code in a function definition:
<
(let ((a (read *standard-input*))
(b (read *standard-input*)))
Line 1,157 ⟶ 1,428:
((> 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,166 ⟶ 1,437:
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,193 ⟶ 1,464:
JMP start
one: 1</
=={{header|D}}==
<
import std.stdio, std.conv, std.string;
Line 1,213 ⟶ 1,484:
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,225 ⟶ 1,496:
[ [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,252 ⟶ 1,523:
:''Slightly different than the [[#Pascal|Pascal]] example''
<
{$APPTYPE CONSOLE}
Line 1,263 ⟶ 1,534:
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,274 ⟶ 1,545:
elif a>b then writeln(a, " > ", b)
fi
corp</
=={{header|Dyalect}}==
Line 1,280 ⟶ 1,551:
{{trans|Clipper}}
<
if a < b {
"A is less than B"
Line 1,288 ⟶ 1,559:
"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,309 ⟶ 1,580:
if a > b
print "greater"
.</
=={{header|ECL}}==
<syntaxhighlight lang="ecl">
CompareThem(INTEGER A,INTEGER B) := FUNCTION
Result := A <=> B;
Line 1,322 ⟶ 1,593:
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,380 ⟶ 1,651:
[ 18 ] AF [ - character ]
EZPF [ begin execution ]</
=={{header|Efene}}==
Line 1,386 ⟶ 1,657:
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,415 ⟶ 1,686:
compare(4, 5)
}
</syntaxhighlight>
=={{header|Eiffel}}==
<
APPLICATION
inherit
Line 1,447 ⟶ 1,718:
end
end
end</
=={{header|Elena}}==
ELENA 4.x:
<
public program()
Line 1,466 ⟶ 1,737:
if (a > b)
{ console.printLine(a," is greater than ",b) }
}</
=={{header|Elixir}}==
<
{b,_} = IO.gets("Enter your second integer: ") |> Integer.parse
Line 1,479 ⟶ 1,750:
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,489 ⟶ 1,760:
((< 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,514 ⟶ 1,813:
end.
</syntaxhighlight>
=={{header|Euphoria}}==
<
integer a,b
Line 1,531 ⟶ 1,830:
puts(1,"grater then")
end if
puts(1," b")</
=={{header|Excel}}==
Line 1,537 ⟶ 1,836:
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,555 ⟶ 1,854:
| 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,576 ⟶ 1,875:
Uses Env.cur to access stdin and stdout.
<
{
public static Void main ()
Line 1,598 ⟶ 1,897:
}
}
</syntaxhighlight>
=={{header|Fermat}}==
<
?a;
?b;
Line 1,607 ⟶ 1,906:
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,623 ⟶ 1,922:
> 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,630 ⟶ 1,929:
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,653 ⟶ 1,952:
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,664 ⟶ 1,963:
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,679 ⟶ 1,978:
end if
end</
In ISO Fortran 90 or later you can use symbolic relational operators (<, >, ==, etc.)
<
integer :: a, b
read(*,*) a, b
Line 1,694 ⟶ 1,993:
end if
end program compare</
=={{header|friendly interactive shell}}==
<
read b
Line 1,728 ⟶ 2,005:
else if test $a -eq $b
echo Equal
end</
=={{header|Frink}}==
All integers in Frink can be arbitrarily large.
<
if a<b
println["$a < $b"]
Line 1,739 ⟶ 2,016:
println["$a == $b"]
if a>b
println["$a > $b"]</
=={{header|FunL}}==
<
a = readInt()
Line 1,749 ⟶ 2,026:
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,855 ⟶ 2,074:
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,876 ⟶ 2,095:
==="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,892 ⟶ 2,111:
=={{header|Harbour}}==
<
IF a < b
Line 1,902 ⟶ 2,121:
ENDIF
RETURN</
=={{header|Haskell}}==
<
myCompare a b
| a < b = "A is less than B"
Line 1,914 ⟶ 2,133:
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,935 ⟶ 2,154:
if a = b
println a + " is equal to " + b
endif</
=={{header|HicEst}}==
<
IF (a < b) THEN
Line 1,946 ⟶ 2,165:
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,960 ⟶ 2,179:
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,986 ⟶ 2,205:
print(a, " is greater than ", b)
end
}</
=={{header|Icon}} and {{header|Unicon}}==
<
until integer(a) do {
Line 2,004 ⟶ 2,223:
write(a," = ", a = b)
write(a," > ", a > b)
end</
{{out}}
Line 2,014 ⟶ 2,233:
=={{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 2,020 ⟶ 2,239:
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 2,048 ⟶ 2,267:
} catch(IOException e) { }
}
}</
=={{header|JavaScript}}==
<
// Using type coercion
function compare(a, b) {
Line 2,078 ⟶ 2,297:
}
}
</syntaxhighlight>
=={{header|Joy}}==
<syntaxhighlight lang=
DEFINE
prompt == "Please enter a number and <Enter>: " putchars;
Line 2,104 ⟶ 2,323:
=={{header|jq}}==
<
# otherwise an "uncomparable" message is emitted.
Line 2,119 ⟶ 2,338:
end ;
compare</
Examples;
<syntaxhighlight lang="jq">
$ jq -s -r -f Integer_comparison.jq
1 2
Line 2,130 ⟶ 2,349:
1 "a"
1 is uncomparable to a
</syntaxhighlight>
=={{header|Julia}}==
<
int1 = readline(stdin)
int2 = readline(stdin)
Line 2,143 ⟶ 2,362:
int2)
end
</
<pre>julia> compare()
3
Line 2,151 ⟶ 2,370:
=={{header|Kotlin}}==
<syntaxhighlight lang
val n1 = readLine()!!.toLong()
val n2 = readLine()!!.toLong()
Line 2,160 ⟶ 2,379:
else -> ""
})
}</
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def compare
{lambda {:a :b}
Line 2,180 ⟶ 2,399:
{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,192 ⟶ 2,428:
#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,199 ⟶ 2,435:
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,241 ⟶ 2,444:
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,271 ⟶ 2,474:
end switch
end if
end if</
=={{header|LLVM}}==
Line 2,278 ⟶ 2,481:
{{libheader|cstdlib}}
<
;e means little endian
;p: { pointer size : pointer abi : preferred alignment for pointers }
Line 2,369 ⟶ 2,572:
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,383 ⟶ 2,586:
==={{header|avec SI}}===
<
ENTIER A, B
LIRE ['Entrez un premier entier:',U] A
Line 2,395 ⟶ 2,598:
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,409 ⟶ 2,612:
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,420 ⟶ 2,623:
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,430 ⟶ 2,633:
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,440 ⟶ 2,643:
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,462 ⟶ 2,665:
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,480 ⟶ 2,683:
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,501 ⟶ 2,704:
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,512 ⟶ 2,715:
((==) ("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,532 ⟶ 2,735:
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,549 ⟶ 2,752:
>
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,637 ⟶ 2,840:
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,658 ⟶ 2,861:
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 2,679 ⟶ 2,882:
InOut.WriteInt(B, 1);
InOut.WriteLn
END IntCompare.</
=={{header|Modula-3}}==
<
FROM IO IMPORT Put, GetInt;
Line 2,699 ⟶ 2,902:
Put(Int(a) & " is greater than " & Int(b) & "\n");
END;
END Main.</
=={{header|MUMPS}}==
<syntaxhighlight lang="text">INTCOMP
NEW A,B
INTCOMPREAD
Line 2,712 ⟶ 2,915:
IF A>B WRITE !,A," is greater than ",B
KILL A,B
QUIT</
{{out}}
<pre>USER>d INTCOMP^ROSETTA
Line 2,731 ⟶ 2,934:
=={{header|Nanoquery}}==
<
first = int(input())
print "enter second integer: "
Line 2,742 ⟶ 2,945:
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,776 ⟶ 2,979:
}
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 2,794 ⟶ 2,997:
return
</syntaxhighlight>
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(print "Please enter the first number: ")
(set 'A (int (read-line)))
Line 2,809 ⟶ 3,012:
(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,821 ⟶ 3,024:
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,848 ⟶ 3,051:
Pop $0
FunctionEnd
</syntaxhighlight>
=== Using [http://nsis.sourceforge.net/LogicLib LogicLib] (bundled library) ===
{{libheader|LogicLib}}
<
Function IntegerComparison
Push $0
Line 2,870 ⟶ 3,073:
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,898 ⟶ 3,110:
Out.Ln;
END;
END Compare.</
=={{header|Objeck}}==
<
bundle Default {
class IntCompare {
Line 2,922 ⟶ 3,134:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
if a < b then "A is less than B"
else if a > b then "A is greater than B"
Line 2,934 ⟶ 3,146:
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,947 ⟶ 3,159:
elseif (a < b)
disp("a less than b");
endif</
=={{header|Oforth}}==
<
: cmpInt
Line 2,960 ⟶ 3,172:
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,980 ⟶ 3,192:
; manual user input:
(print (compare (read) (read)))
</syntaxhighlight>
=={{header|Oz}}==
<
import
Application(exit)
Line 3,010 ⟶ 3,222:
{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 3,033 ⟶ 3,245:
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 3,040 ⟶ 3,265:
Separate tests for less than, greater than, and equals
<
my $f = shift;
my $s = shift;
Line 3,052 ⟶ 3,277:
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 3,082 ⟶ 3,307:
<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,106 ⟶ 3,350:
return 0;
]</
=={{header|PHP}}==
<
echo "Enter an integer [int1]: ";
Line 3,135 ⟶ 3,379:
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,149 ⟶ 3,393:
((= A B) "equal to")
(T "less than") )
" the second." ) ) )</
{{out}}
<pre>Please enter two values: 4 3
Line 3,155 ⟶ 3,399:
=={{header|Pike}}==
<
if(argc != 3){
write("usage: `pike compare-two-ints.pike <x> <y>` where x and y are integers.\n");
Line 3,171 ⟶ 3,415:
write(a + " is equal to " + b + "\n");
}
}</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare (a, b) fixed binary;
Line 3,184 ⟶ 3,428:
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,197 ⟶ 3,441:
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,216 ⟶ 3,460:
;;; Read numbers and call comparison procedure
compare_integers(itemrep(), itemrep());</
=={{header|PowerShell}}==
<
$b = [int] (Read-Host b)
Line 3,228 ⟶ 3,472:
} 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,261 ⟶ 3,484:
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,268 ⟶ 3,491:
{{works with|Python|2.x only, not 3.x}}
<
import sys
try:
Line 3,282 ⟶ 3,505:
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,288 ⟶ 3,511:
=={{header|Quackery}}==
<
input quackery cr
say "The first number is "
Line 3,295 ⟶ 3,518:
2dup < if [ say "smaller than" ] ]
2drop
say " the second number." cr</
{{out}}
Line 3,317 ⟶ 3,540:
=={{header|R}}==
<
a <- scan(what=numeric(0), nmax=1)
print("insert number b")
Line 3,327 ⟶ 3,550:
} 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,338 ⟶ 3,561:
(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,353 ⟶ 3,576:
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}}==
<
input: a
input: b
Line 3,375 ⟶ 3,598:
when a = b:
output: "a is equal to b"
esac</
=={{header|Raven}}==
<
expect trim 1.1 prefer as $a
"Enter the second number: " print
Line 3,385 ⟶ 3,608:
$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,402 ⟶ 3,625:
]
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,418 ⟶ 3,641:
end if
end if
</syntaxhighlight>
=={{header|Red}}==
{{trans|REBOL}}
<
Title: "Comparing Two Integers"
URL: http://rosettacode.org/wiki/Comparing_two_integers
Line 3,435 ⟶ 3,658:
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,449 ⟶ 3,672:
let b = int_of_string(Sys.argv[3])
Js.log(my_compare(a, b))</
{{out}}
<pre>
Line 3,464 ⟶ 3,687:
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,493 ⟶ 3,716:
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,521 ⟶ 3,744:
=={{header|Ring}}==
<
Func Compare a,b
if a < b
Line 3,530 ⟶ 3,753:
See "A equals B"
ok
</syntaxhighlight>
=={{header|Rockstar}}==
Line 3,536 ⟶ 3,759:
Minimized Rockstar:
<syntaxhighlight lang="rockstar">
(Get two numbers from user)
Listen to Number One
Line 3,551 ⟶ 3,774:
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,566 ⟶ 3,789:
If your soul is smaller than my words,
Say "The second was bigger".
</syntaxhighlight>
=={{header|RPG}}==
Line 3,573 ⟶ 3,796:
CALL rc_intcmp (x'00000000' x'00000001')
<syntaxhighlight lang="rpg">
h dftactgrp(*no)
Line 3,596 ⟶ 3,819:
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,617 ⟶ 3,854:
when 0; puts "#{a} is equal to #{b}"
when +1; puts "#{a} is greater than #{b}"
end</
Example '''input''' and output:
Line 3,635 ⟶ 3,872:
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,658 ⟶ 3,895:
# 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,688 ⟶ 3,917:
println!("{} is greater than {}" , a , b)
};
}</
=={{header|SAS}}==
<
data _null_;
input a b;
Line 3,713 ⟶ 3,942:
1 1
;
run;</
=={{header|Scala}}==
<
def main(args: Array[String]): Unit = {
val a=Console.readInt
Line 3,727 ⟶ 3,956:
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,759 ⟶ 3,988:
writeln(a <& " is greater than " <& b);
end if;
end func;</
=={{header|SenseTalk}}==
<
Put It into A
Line 3,781 ⟶ 4,010:
If A is Equal to B
Put A& " is Equal to " &B
End If</
=={{header|Sidef}}==
<
var b = read("b: ", Number);
Line 3,795 ⟶ 4,024:
elsif (a > b) {
say 'Greater';
}</
{{out}}
<pre>
Line 3,805 ⟶ 4,034:
=={{header|Slate}}==
<
( a > b ) ifTrue: [ inform: 'a greater than b\n' ].
Line 3,811 ⟶ 4,040:
( 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,826 ⟶ 4,055:
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,833 ⟶ 4,062:
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,852 ⟶ 4,081:
} else {
print("either a or b or both are NaN");
}</
=={{header|SQL}}==
{{works with|Oracle}}
<
drop table test;
Line 3,878 ⟶ 4,107:
from test
where a > b;
</syntaxhighlight>
<pre>
Line 3,900 ⟶ 4,129:
{{works with|Db2 LUW}}
With SQL only:
<
CREATE TABLE TEST (
VAL1 INT,
Line 3,916 ⟶ 4,145:
END COMPARISON
FROM TEST;
</syntaxhighlight>
Output:
<pre>
Line 3,948 ⟶ 4,177:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 3,966 ⟶ 4,195:
CALL COMPARISON(2, 2) @
CALL COMPARISON(2, 1) @
</syntaxhighlight>
Output:
<pre>
Line 4,018 ⟶ 4,247:
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 4,038 ⟶ 4,267:
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 4,054 ⟶ 4,283:
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 4,078 ⟶ 4,307:
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 4,089 ⟶ 4,318:
This is not how one would write this in Tcl, but for the sake of clarity:
<
gets stdin x
Line 4,096 ⟶ 4,325:
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 4,102 ⟶ 4,331:
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 4,109 ⟶ 4,338:
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,117 ⟶ 4,346:
% 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,149 ⟶ 4,357:
1 1 compare-integers
2 1 compare-integers
1 2 compare-integers</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
Line 4,163 ⟶ 4,371:
IF (i1<i2) PRINT i1," is less than ",i2
IF (i1>i2) PRINT i1," is greater than ",i2
</syntaxhighlight>
=={{header|UNIX Shell}}==
Line 4,170 ⟶ 4,378:
{{works with|ksh93}}
<
# tested with ksh93s+
Line 4,191 ⟶ 4,399:
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,197 ⟶ 4,405:
{{works with|pdksh}}
<
# tested with pdksh
Line 4,216 ⟶ 4,424:
fi
exit 0</
----
Line 4,222 ⟶ 4,430:
{{works with|Bash}}
<
if [ $a -gt $b ]; then comparison="greater than"
Line 4,230 ⟶ 4,438:
fi
echo "${a} is ${comparison} ${b}"</
=={{header|Ursa}}==
<
out "enter first integer: " console
set first (in int console)
Line 4,247 ⟶ 4,455:
if (> first second)
out first " is greater than " second endl console
end if</
=={{header|V}}==
<
[ [>] ['less than' puts]
[<] ['greater than' puts]
Line 4,261 ⟶ 4,469:
less than
|2 2 compare
is equal</
=={{header|Vala}}==
<
void main(){
int a;
Line 4,282 ⟶ 4,490:
stdout.printf("%d is greater than %d\n", a, b);
}
</syntaxhighlight>
=={{header|Wart}}==
<
b <- (read)
prn (if (a < b)
Line 4,360 ⟶ 4,500:
: "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,384 ⟶ 4,524:
}
System.print()
}</
{{out}}
Line 4,407 ⟶ 4,547:
=={{header|XLISP}}==
<
(DISPLAY "Enter two integers separated by a space.")
(NEWLINE)
Line 4,416 ⟶ 4,556:
((> 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,427 ⟶ 4,567:
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,442 ⟶ 4,582:
</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,452 ⟶ 4,592:
greater
</pre>
|