Integer comparison: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(10 intermediate revisions by 8 users not shown) | |||
Line 694:
=={{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}}===
Line 702 ⟶ 708:
IF a = b THEN PRINT a, " is equal to ", b
IF a > b THEN PRINT a, " is greater than ", b</syntaxhighlight>
{{works with|QuickBasic|4.5}}
Line 713 ⟶ 718:
PRINT "b"</syntaxhighlight>
==={{header|
<syntaxhighlight lang="
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}}===
Line 744 ⟶ 849:
190 END SELECT</syntaxhighlight>
==={{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}}===
Line 769 ⟶ 899:
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}}===
Line 776 ⟶ 927:
IF x = y THEN PRINT x; " is equal to "; y
IF x > y THEN PRINT x; " is greater than "; y</syntaxhighlight>
==={{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}}===
Line 785 ⟶ 977:
IF x > y THEN PRINT x; " is greater than "; y
END</syntaxhighlight>
==={{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}}===
Line 793 ⟶ 1,051:
if x > y then print x, " is greater than ", y : fi</syntaxhighlight>
==={{
<syntaxhighlight lang="
20 PRINT a;" is ";("less than " AND (a<b));("equal to " AND (a=b));("greather than " AND (a>b));b</syntaxhighlight>
=={{header|Batch File}}==
Line 819 ⟶ 1,072:
B: 3
5 is greater than 3</pre>
=={{header|bc}}==
Line 837 ⟶ 1,082:
if (a == b) print "a is equal to b\n"
quit</syntaxhighlight>
=={{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 1,493 ⟶ 1,754:
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}}==
Line 1,695 ⟶ 1,984:
end program compare</syntaxhighlight>
=={{header|friendly interactive shell}}==
Line 1,751 ⟶ 2,018:
println( "$a is $c $b." )</syntaxhighlight>
=={{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}}==
Line 2,151 ⟶ 2,360:
=={{header|Kotlin}}==
<syntaxhighlight lang="
val n1 = readLine()!!.toLong()
val n2 = readLine()!!.toLong()
Line 2,180 ⟶ 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>
Line 2,199 ⟶ 2,425:
Number 1 is the same as Number 2
Number 1 is not greater than Number 2</pre>
=={{header|LIL}}==
Line 2,870 ⟶ 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>
Line 3,248 ⟶ 3,450:
Write-Host $a is greater than $b`.
}</syntaxhighlight>
=={{header|Python}}==
Line 3,616 ⟶ 3,797:
*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}}==
Line 3,678 ⟶ 3,873:
# I hope you can figure this out
puts "#{a} is #{dispatch[a<=>b]} #{b}"</syntaxhighlight>
=={{header|Rust}}==
Line 4,137 ⟶ 4,324:
% foreach {o s} {< "less than" > "greater than" == equal} {if [list $i $o $j] {puts "$i is $s $j"}}
5 is greater than 4</syntaxhighlight>
=={{header|Toka}}==
Line 4,302 ⟶ 4,468:
}
</syntaxhighlight>
=={{header|Wart}}==
Line 4,382 ⟶ 4,480:
=={{header|Wren}}==
<syntaxhighlight lang="
System.print("Enter anything other than a number to quit at any time.\n")
Line 4,471 ⟶ 4,569:
greater
</pre>
|