A+B: Difference between revisions

6,566 bytes added ,  1 month ago
(Add GDScript)
(28 intermediate revisions by 21 users not shown)
Line 424:
lv_output = p_first + p_second.
write : / lv_output.</syntaxhighlight>
 
=={{header|Acornsoft Lisp}}==
{{trans|Common Lisp}}
 
<pre>
Evaluate: (print (plus (read) (read)))
3 2
5
</pre>
 
=={{header|Action!}}==
Line 1,403 ⟶ 1,412:
=={{header|AWK}}==
<syntaxhighlight lang="awk">{print $1 + $2}</syntaxhighlight>
 
=={{header|BabyCobol}}==
<syntaxhighlight lang="cobol">
* NB: COBOL's ACCEPT does not work with multiple identifiers
IDENTIFICATION DIVISION.
PROGRAM-ID. PLUS.
DATA DIVISION.
01 A PICTURE IS S9999.
01 B LIKE A.
PROCEDURE DIVISION.
DISPLAY "Enter two numbers: " WITH NO ADVANCING.
ACCEPT A B.
ADD A TO B.
DISPLAY "A+B =" B.
</syntaxhighlight>
 
=={{header|BASIC}}==
Line 1,467 ⟶ 1,491:
PRINT VAL LEFT$(q$,space%-1) + VAL MID$(q$,space%+1)
UNTIL FALSE</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|Applesoft BASIC}}
{{works with|GW-BASIC}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 CLS : rem 10 HOME for Applesoft BASIC
20 PRINT "ENTER TWO NUMBERS, SEPARATED BY A SPACE: ";
30 INPUT X$
40 I = 1 : N = LEN(X$)
50 IF MID$(X$, I, 1) <> " " AND I < N THEN I = I + 1 : GOTO 50
60 A = VAL(LEFT$(X$, I))
70 B = VAL(RIGHT$(X$, N - 1))
80 PRINT A + B
90 END</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 1,495 ⟶ 1,536:
Print "Press any key to quit the program"
Sleep</syntaxhighlight>
 
==={{header|GW-BASIC}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|FUZE BASIC}}===
Line 1,515 ⟶ 1,559:
<syntaxhighlight lang="lb">input, n$
print eval(word$(n$,1);" + ";word$(n$,2))</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|IS-BASIC}}
{{works with|MSX Basic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
{{works with|Yabasic}}
<syntaxhighlight lang="qbasic">10 PRINT "ENTER NUMBER A";
20 INPUT A
30 PRINT "ENTER NUMBER B";
40 INPUT B
50 PRINT A+B
60 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|OxygenBasic}}===
Line 1,539 ⟶ 1,606:
50 GOTO 30
60 PRINT VAL A$( TO I-1)+VAL A$(I+1 TO )</syntaxhighlight>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="SmallBASIC">
input "Enter number A: "; a
input "Enter number B: "; b
print "A + B = "; a + b
</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
Line 1,587 ⟶ 1,661:
Loop
Return (b@)</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "A+B"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
a$ = INLINE$("Enter integer A: ")
a = SLONG(a$)
b$ = INLINE$("Enter integer B: ")
b = SLONG(b$)
DO WHILE 1
IF ABS(a) > 1000 OR ABS(b) > 1000 THEN
PRINT "Both integers must be in the interval [-1000..1000] - try again."
PRINT
ELSE
PRINT "Their sum is"; a + b
EXIT DO
END IF
LOOP
END FUNCTION
END PROGRAM</syntaxhighlight>
 
=={{header|Batch File}}==
Line 1,735 ⟶ 1,833:
<syntaxhighlight lang="brat">numbers = g.split[0,1].map(:to_i)
p numbers[0] + numbers[1] #Prints the sum of the input</syntaxhighlight>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/String .
:import std/Number .
:import std/Char C
 
main (split-by (C.eq? ' ')) → &(add ⋔ string→number)
</syntaxhighlight>
 
=={{header|Burlesque}}==
Line 2,231 ⟶ 2,339:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">a$ = input
ia$ = 1input
repeat
while i < len a$ and substr a$ i 1 <> " "
i += 1
until i > len a$ or substr a$ i 1 = " "
.
a = number substr a$ 1 i
b = number substr a$ i -199
print a + b
</syntaxhighlight>
Line 2,494 ⟶ 2,603:
 
=={{header|Elena}}==
ELENA 56.0 :
<syntaxhighlight lang="elena">import extensions;
Line 2,513 ⟶ 2,622:
console.printLine(console.readLine()
.split()
.selectBy(mssgconst toInt<convertorOpintConvertOp>[1])
.summarize())
}</syntaxhighlight>
Line 2,545 ⟶ 2,654:
(b (cadr numbers)))
(message "%d" (+ a b)))</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun main = int by List args
text input = when(args.length == 1, args[0], ask(text, "Enter n integers separated by a space: "))
int sum, count
for each text word in input.split(" ")
word = word.trim()
if word.isEmpty() do continue end # ignore empty words
int nr = int!word # this can raise an exception
if abs(nr) > 1000
Event.error(0, "Integers must be in the interval [-1000, 1000]").raise()
end
sum += nr
++count
end
if count < 2 do Event.error(1, "At least two integers must be provided").raise() end
writeLine("The sum of " + count + " integers is: " + sum)
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\Aplusb.emal " 9 8 "
The sum of 2 integers is: 17
</pre>
 
=={{header|Emojicode}}==
Line 2,584 ⟶ 2,720:
 
=={{header|Euler}}==
'''begin'''
<syntaxhighlight lang="euler">
'''out''' '''in''' + '''in'''
begin
'''end''' $
out in + in
end $
</syntaxhighlight>
 
=={{header|Euler Math Toolbox}}==
Line 2,677 ⟶ 2,811:
 
PAUSE</syntaxhighlight>
 
=={{header|Fennel}}==
{{trans|Lua}}
<syntaxhighlight lang="fennel">
(let [(a b) (io.read :*number :*number)]
(print (+ a b)))
</syntaxhighlight>
 
=={{header|Fhidwfe}}==
Line 2,918 ⟶ 3,059:
extends Node
 
@export_multilineexport var input: String:
set(value):
input = value # Save the input field
 
var fields := value.split(" ", false) # Split by spaces
# For every line of input, append the sum as a string to the out array.
if len(fields) == 2: # Basic input validation
var out: Array[String] = [] # Array to store the lines of output
output = str(int(fields[0]) + int(fields[1]))
for line in value.split("\n"):
else: # Invalid input
var parts := line.split(" ", false)
varoutput sum := 0""
for part in parts:
sum += int(part)
out.append(str(sum))
 
# Update the output property
output = "\n".join(out)
 
@export_multilineexport var output: String
</syntaxhighlight>
 
Line 3,171 ⟶ 3,306:
line <- getLine
print $ sum $ map cast $ words line</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(+ (to-num (prompt "Enter first number: "))
(to-num (prompt "Enter second number: ")))</syntaxhighlight>
 
=={{header|J}}==
Line 3,181 ⟶ 3,320:
<syntaxhighlight lang="j">+/". (1!:1(3))-.LF</syntaxhighlight>
2) Here's a little script, called "a+b.ijs":
<syntaxhighlight lang="j">#!/Applications/j602usr/bin/jconsoleijconsole
echo +/". (1!:1(3))-.LF
exit ''</syntaxhighlight>
Line 3,187 ⟶ 3,326:
<syntaxhighlight lang="bash">echo 2 3 | ./a+b.ijs
5</syntaxhighlight>
 
Note: under OSX, you should probably install a symbolic link at /usr/bin/ijconsole which links to something like /Applications/j602/bin/jconsole.
 
=={{header|Java}}==
The task description is somewhat vague, if the 'input stream' is in reference to the data provided at the command line, then Java will parse these values into a <code>String</code> array, accessible via the <kbd>args</kbd> parameter of the <code>main</code> method.
<syntaxhighlight lang="java">
public static void main(String[] args) {
int A = Integer.parseInt(args[0]);
int B = Integer.parseInt(args[1]);
System.out.println(A + B);
}
</syntaxhighlight>
If the 'input stream' is the standard-in, you can use the following.<br />
The <code>Scanner</code> class can be used to "scan" the standard-in stream from left to right, returning the next value requested.
<syntaxhighlight lang="java">
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
System.out.println(A + B);
}
</syntaxhighlight>
If the 'input stream' is referring to any abstraction, then you can use the following.<br />
In this example, the data is considered to be under a specific charset, so the integer values are parsed using UTF-8.
<syntaxhighlight lang="java">
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
</syntaxhighlight>
<syntaxhighlight lang="java">
int sum(InputStream input) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
reader.close();
/* split parameter here is a regex */
String[] values = line.split(" +");
int A = Integer.parseInt(values[0]);
int B = Integer.parseInt(values[1]);
return A + B;
}
</syntaxhighlight>
<br />
An alternate implemenation
<syntaxhighlight lang="java">import java.util.Scanner;
 
Line 3,981 ⟶ 4,165:
20 INPUT "ENTER NUMBER B: ",B
30 PRINT A+B</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
input | parse "{a} {b}" | first | values | into int | math sum
</syntaxhighlight>
 
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main
imports native.io{input.hear,output.say}
 
vals a=hear(Int),b=hear(Int)
say(a+b)
 
end
</syntaxhighlight>
 
=={{header|Nyquist}}==
Line 4,334 ⟶ 4,534:
</pre>
=== GUI version ===
{{libheader|Phix/pGUI}}
<small>(The above console version is now just a comment in the distributed file.)</small>
<!--<syntaxhighlight lang="phix">(phixonline)-->
Line 5,105 ⟶ 5,306:
 
More idiomatically:
<syntaxhighlight lang="ruby">say read(String).words»to_i»()»«+»</syntaxhighlight>
 
Explicit summation:
Line 5,774 ⟶ 5,975:
End With
end if</syntaxhighlight>
 
=={{header|Vedit macro language}}==
This version implements the task as specified in the task description.
<syntaxhighlight lang="vedit">// Input two values on single line in text format
Get_Input(10, "Enter two integers separated by a space: ")
 
// Extract two numeric values from the text
Buf_Switch(Buf_Free)
Reg_Ins(10)
BOF
#1 = Num_Eval(ADVANCE)
#2 = Num_Eval()
Buf_Quit(OK)
 
// Calculate and display the results
Num_Type(#1 + #2)</syntaxhighlight>
 
A simpler version that prompts for the two numbers separately:
<syntaxhighlight lang="vedit">#1 = Get_Num("Enter number A: ")
#2 = Get_Num("Enter number B: ")
Num_Type(#1 + #2)</syntaxhighlight>
 
=={{header|Verilog}}==
Line 5,893 ⟶ 6,115:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
while (true) {
62

edits