A+B: Difference between revisions

9,275 bytes added ,  1 month ago
(33 intermediate revisions by 24 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,582 ⟶ 2,718:
END PROGRAM
</syntaxhighlight>
 
=={{header|Euler}}==
'''begin'''
'''out''' '''in''' + '''in'''
'''end''' $
 
=={{header|Euler Math Toolbox}}==
Line 2,670 ⟶ 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,902 ⟶ 3,050:
<suma> =, eA + eB
</syntaxhighlight>
 
=={{header|GDScript}}==
Requires Godot 4. Runs as a tool script using the input and output properties. Does not check for zero lines of input.
 
<syntaxhighlight lang="gdscript">
@tool
extends Node
 
@export var input: String:
set(value):
input = value # Save the input field
 
var fields := value.split(" ", false) # Split by spaces
if len(fields) == 2: # Basic input validation
output = str(int(fields[0]) + int(fields[1]))
else: # Invalid input
output = ""
 
@export var output: String
</syntaxhighlight>
 
Line 3,138 ⟶ 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,148 ⟶ 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,154 ⟶ 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,507 ⟶ 3,724:
Several boxes can be created in the wiki page
with any valid lambdatalk expressions.
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fn.print(First number:\s)
$a = fn.int(fn.input())
 
fn.print(Second number:\s)
$b = fn.int(fn.input())
 
fn.println(Result: parser.op($a + $b))
</syntaxhighlight>
 
Line 3,937 ⟶ 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,290 ⟶ 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 4,406 ⟶ 4,651:
<syntaxhighlight lang="pli">get (a, b);
put (a+b);</syntaxhighlight>
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">
To run:
Start up.
Read a number from the console.
Read another number from the console.
Output the sum of the number and the other number.
Wait for the escape key.
Shut down.
 
To output the sum of a number and another number:
If the number is not valid, write "Invalid input" to the console; exit.
If the other number is not valid, write "Invalid input" to the console; exit.
Write the number plus the other number then " is the sum." to the console.
 
To decide if a number is valid:
If the number is not greater than or equal to -1000, say no.
If the number is not less than or equal to 1000, say no.
Say yes.
</syntaxhighlight>
 
=={{header|Pony}}==
Line 5,040 ⟶ 5,306:
 
More idiomatically:
<syntaxhighlight lang="ruby">say read(String).words»to_i»()»«+»</syntaxhighlight>
 
Explicit summation:
Line 5,159 ⟶ 5,425:
 
Domain:[http://fricas.github.io/api/SExpression.html?highlight=lisp SExpression]
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "aplusb" )
@( description, "A+B - in programming contests, classic problem, which is given so" )
@( description, "contestants can gain familiarity with online judging system being used. " )
@( description, "A+B is one of few problems on contests, which traditionally lacks fabula." )
@( description, "Given 2 integer numbers, A and B. One needs to find their sum. " )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/A%2BB" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure aplusb is
s : string;
a : integer;
b : integer;
begin
s := get_line;
a := numerics.value( strings.field( s, 1, ' ' ) );
b := numerics.value( strings.field( s, 2, ' ' ) );
? a+b;
end aplusb;</syntaxhighlight>
 
=={{header|SPARK}}==
Line 5,682 ⟶ 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,801 ⟶ 6,115:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
while (true) {
62

edits