# A+B

A+B
You are encouraged to solve this task according to the task description, using any language you may know.

A+B - in programming contests, classic problem, which is given so contestants can gain familiarity with online judging system being used.

Problem statement
Given 2 integer numbers, A and B. One needs to find their sum.

Input data
Two integer numbers are written in the input stream, separated by space.
${\displaystyle (-1000\leq A,B\leq +1000)}$
Output data
The required output is one integer: the sum of A and B.
Example:
Input Output
2 2 4
3 2 5

## 0815

<lang 0815>|x|+%</lang>

## ABAP

<lang ABAP>report z_sum_a_b. data: lv_output type i. selection-screen begin of block input.

 parameters:
p_first type i,
p_second type i.


selection-screen end of block input.

at selection-screen output.

 %_p_first_%_app_%-text  = 'First Number: '.
%_p_second_%_app_%-text = 'Second Number: '.


start-of-selection.

 lv_output = p_first + p_second.
write : / lv_output.</lang>


  A, B : Integer;


begin

  Ada.Integer_Text_Io.Get (Item => A);


procedure A_Plus_B is

  type Small_Integers is range -2_000 .. +2_000;
subtype Input_Values is Small_Integers range -1_000 .. +1_000;
package IO is new Ada.Text_IO.Integer_IO (Num => Small_Integers);
A, B : Input_Values;


begin

  IO.Get (A);
IO.Get (B);
IO.Put (A + B, Width => 4, Base => 10);


end A_Plus_B;</lang>

## ALGOL 68

Translation of: python
Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny

### Console

1 2


Output:

         +3


### File

<lang algol68>open(stand in, "input.txt", stand in channel); open(stand out, "output.txt", stand out channel); print((read int + read int))</lang> Input "input.txt":

3 4


Output "output.txt":

         +7


## ANTLR

### Java

<lang> grammar aplusb ;

options { language = Java; }

aplusb : (WS* e1=Num WS+ e2=Num NEWLINE {System.out.println($e1.text + " + " +$e2.text + " = " + (Integer.parseInt($e1.text) + Integer.parseInt($e2.text)));})+ ; Num : '-'?('0'..'9')+ ; WS : (' ' | '\t') ; NEWLINE : WS* '\r'? '\n'

       ;


</lang> Produces:

>java Test
1 2
23 89
13 567
-75 6
-75 -29
^Z
1 + 2 = 3
23 + 89 = 112
13 + 567 = 580
-75 + 6 = -69
-75 + -29 = -104


## Argile

Translation of: C
Works with: Argile version 1.0.0

<lang Argile>(: Standard input-output streams :) use std, array Cfunc scanf "%d%d" (&val int a) (&val int b) printf "%d\n" (a + b)</lang> <lang Argile>(: Input file : input.txt :) (: Output file: output.txt :) use std, array let in = fopen "input.txt" "r" let out = fopen "output.txt" "w" let int x, y. Cfunc fscanf in "%d%d" (&x) (&y) (:fscanf not yet defined in std.arg:) fprintf out "%d\n" (x+y) fclose in fclose out</lang>

## AutoHotkey

<lang AutoHotkey>InputBox, input , A+B, Two integer numbers, separated by space. StringSplit, output, input, %A_Space% msgbox, % output1 . "+" . output2 "=" output1+output2</lang>

## AutoIt

<lang AutoIt>;AutoIt Version: 3.2.10.0 $num = "45 54" consolewrite ("Sum of " &$num & " is: " & sum($num)) Func sum($numbers)

  $numm = StringSplit($numbers," ")
Return $numm[1]+$numm[$numm[0]]  EndFunc</lang> ## AWK <lang awk>{print$1 + $2}</lang> ## Batch File Prompts version <lang dos>::aplusb.cmd @echo off setlocal set /p a="A: " set /p b="B: " set /a c=a+b echo %c% endlocal</lang> All on the commandline version <lang dos>::aplusb.cmd @echo off setlocal set a=%1 set b=%2 set /a c=a+b echo %c% endlocal</lang> Formula on the command line version <lang dos>::aplusb.cmd @echo off setlocal set /a c=%~1 echo %c% endlocal</lang> Example of 'Formula on the command line version' >aplusb 123+456 579 >aplusb "1+999" 1000  Parse the input stream version (thanks to Tom Lavedas on alt.msdos.batch.nt) <lang dos>::aplusb.cmd @echo off setlocal set /p a="Input stream: " call :add %a% echo %res% endlocal goto :eof add set /a res=res+%1 shift if "%1" neq "" goto :add</lang> Example of 'parse the input stream version' >aplusb Input stream: 1234 5678 6912 >aplusb Input stream: 123 234 345 456 567 678 789 890 4082 ## BASIC <lang qbasic>DEFINT A-Z tryagain: backhere = CSRLIN INPUT "", i$ i$= LTRIM$(RTRIM$(i$)) where = INSTR(i$, " ") IF where THEN  a = VAL(LEFT$(i$, where - 1)) b = VAL(MID$(i$, where + 1)) c = a + b LOCATE backhere, LEN(i$) + 1
PRINT c


ELSE

   GOTO tryagain


END IF</lang>

<lang basic256>dim a(2) input "Enter two numbers seperated by a space?", t$a = explode(t$," ") print t$+ " " + (a[0] + a[1])</lang> ## BBC BASIC <lang bbc> REPEAT  hereY% = VPOS INPUT LINE "" q$
hereX% = LEN(q$) + 1 WHILE LEFT$(q$, 1) = " " q$ = MID$(q$, 2)
ENDWHILE
space% = INSTR(q$, " ") IF space% THEN a = VAL(LEFT$(q$, space% - 1)) b = VAL(MID$(q$, space% + 1)) PRINT TAB(hereX%, hereY%) ; a + b ENDIF UNTIL FALSE</lang>  That seems overly complicated. What's wrong with: <lang bbc> REPEAT  INPUT LINE "" q$
space% = INSTR(q$," ") PRINT VAL LEFT$(q$,space%-1) + VAL MID$(q$,space%+1) UNTIL FALSE</lang>  ## Befunge <lang befunge>&&+.@</lang> ## Bracmat filter is a pattern that checks that input is a non-fractional number not less than -1000 and not greater than 1000. The filter is applied to each input. <lang bracmat>( out$ ( put$"Enter two integer numbers between -1000 and 1000:"  & (filter=~/#%:~<-1000:~>1000) & get':(!filter:?a) (!filter:?b) & !a+!b | "Invalid input. Try again" )  );</lang> ## Brainf***  This example is incorrect. Please fix the code and remove this message.Details: It only handles single-digit inputs and output. <lang brainf***>, Read first number >,, Eat separator and read second number [<+>-] Add ASCII values ,++ Use newline to get a 12 [<---->-] Subtract 48 to get back to ASCII <. and print</lang> ## Brat <lang brat>numbers = g.split[0,1].map(:to_i) p numbers[0] + numbers[1] #Prints the sum of the input</lang> ## Burlesque <lang burlesque>ps++</lang> ## C <lang c>// Standard input-output streams 1. include <stdio.h> int main() {  int a, b; scanf("%d%d", &a, &b); printf("%d\n", a + b); return 0;  }</lang> <lang c>// Input file: input.txt // Output file: output.txt 1. include <stdio.h> int main() {  freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); int a, b; scanf("%d%d", &a, &b); printf("%d\n", a + b); return 0;  }</lang> ## C# <lang csharp>using System; using System.Linq; class Program {  static void Main() { Console.WriteLine(Console.ReadLine().Split().Select(int.Parse).Sum()); }  }</lang> Another way (not recommended since it does not work with more than two numbers): <lang csharp>using System; class Program  { static void Main() { start: string input = Console.ReadLine(); int index = input.IndexOf(" "); string num1 = input.Substring(0, index); string num2 = input.Substring(index + 1); int sum = int.Parse(num1) + int.Parse(num2); Console.WriteLine(sum.ToString()); goto start; } }</lang>  ## C++ <lang cpp>// Standard input-output streams 1. include <iostream> using namespace std; void main() {  int a, b; cin >> a >> b; cout << a + b << endl;  }</lang> <lang cpp>// Input file: input.txt // Output file: output.txt 1. include <fstream> using namespace std; int main() {  ifstream in("input.txt"); ofstream out("output.txt"); int a, b; in >> a >> b; out << a + b << endl; return 0;  }</lang> ## Clojure <lang clojure>(println (+ (Integer/parseInt (read-line)) (Integer/parseInt (read-line)))) 3 4 =>7</lang> <lang clojure>(eval (read-string (str "(+ " (read-line) " )") )) 3 3 6</lang> ## CoffeeScript Translation of: JavaScript <lang html4strict><html> <script type="text/javascript" src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script> <script type="text/coffeescript"> a = window.prompt 'enter A number', b = window.prompt 'enter B number', document.getElementById('input').innerHTML = a + ' ' + b sum = parseInt(a) + parseInt(b) document.getElementById('output').innerHTML = sum </script> <body> </body> </html></lang> ## Common Lisp <lang lisp>(write (+ (read) (read)))</lang> ## D ### From Console <lang d>import std.stdio, std.conv, std.string; void main() {  string[] r; try r = readln().split(); catch (StdioException e) r = ["10", "20"];   writeln(to!int(r[0]) + to!int(r[1]));  }</lang> Output: 30 ### From File <lang d>import std.stdio, std.conv, std.string; void main() {  auto fin = File("sum_input.txt", "r"); auto r = fin.readln().split(); auto fout = File("sum_output.txt", "w"); fout.writeln(to!int(r[0]) + to!int(r[1]));  }</lang> ## dc <lang dc>? + psz</lang> The question mark ? reads and executes a line of input. The user must enter a dc program that pushes two numbers to the stack, such as 2 3 or 5 _1. (The user must use underscore _ for negative numbers.) ## Déjà Vu Translation of: Python ### Console <lang dejavu>0 for k in split " " input: + to-num k print</lang> ## Delphi Console version. <lang delphi>program SUM; {$APPTYPE CONSOLE}

uses

 SysUtils;


var

 s1, s2:string;


begin

 ReadLn(s1);
Writeln(StrToIntDef(s1, 0) + StrToIntDef(s2,0));


end.</lang>

## DMS

<lang DMS>number a = GetNumber( "Please input 'a'", a, a ) // prompts for 'a' number b = GetNumber( "Please input 'b'", b, b ) // prompts for 'b' Result( a + b + "\n" )</lang>

## DWScript

Ghetto GUI version <lang delphi>var a := StrToInt(InputBox('A+B', 'Enter 1st number', '0')); var b := StrToInt(InputBox('A+B', 'Enter 2nd number', '0')); ShowMessage('Sum is '+IntToStr(a+b));</lang>

## Eiffel

argument(0) contains the path of the executable - thus we start at argument(1) <lang eiffel> class APPLICATION inherit ARGUMENTS create make feature {NONE} -- Initialization make -- Run application. do print(argument(1).to_integer + argument(2).to_integer) end end </lang>

## Ela

<lang ela>open console list string read

Output:

1 2 3 4 5 6
21

## Elena

<lang elena>#define std'basic'*.

1. define ext'io'*.
1. symbol Console =>

[

   #var anOutput := __wrap(ELineInput, 'program'input).
#var A := anOutput >> Integer.
#var B := anOutput >> Integer.

'program'output << A + B.


].</lang>

## Erlang

<lang erlang>-module(aplusb). -export([start/0]).

start() ->

   case io:fread("","~d~d") of
eof -> ok;
{ok, [A,B]} ->
io:format("~w~n",[A+B]),
start()
end.</lang>


## Euler Math Toolbox

<lang Euler Math Toolbox> >s=lineinput("Two numbers seperated by a blank");

Two numbers seperated by a blank? >4 5


>vs=strtokens(s)

4
5


>vs[1]()+vs[2]()

9


</lang>

## Euphoria

<lang euphoria>include get.e

function snd(sequence s)

   return s[2]


end function

integer a,b

a = snd(get(0)) b = snd(get(0))

printf(1," %d\n",a+b)</lang>

## EGL

<lang EGL> package programs;

// basic program // program AplusB type BasicProgram {} function main() try arg1 string = SysLib.getCmdLineArg(1); arg2 string = SysLib.getCmdLineArg(2); int1 int = arg1; int2 int = arg2; sum int = int1 + int2; SysLib.writeStdout("sum1: " + sum); onException(exception AnyException) SysLib.writeStdout("No valid input. Provide 2 integer numbers as arguments to the program."); end end end </lang>

## F#

<lang fsharp>open System let plus = (fun (a:string) (b:string) -> Console.WriteLine(int(a)+int(b))) (Console.ReadLine()) (Console.ReadLine());;</lang>

## Factor

<lang factor>USING: math.parser splitting ;

a+b ( -- )
   readln " " split1
[ string>number ] bi@ +
number>string print ;</lang>

( scratchpad ) a+b
2 2
4


## UNIX Shell

Works with: Bourne Shell

<lang sh>#!/bin/sh read a b || exit echo expr "$a" + "$b"</lang>

Works with: bash
Works with: ksh93
Works with: pdksh
Works with: zsh

## Ursala

Using standard input and output streams: <lang Ursala>#import std

1. import int
1. executable&

main = %zP+ sum:-0+ %zp*FiNCS+ sep @L</lang> Overwriting a text file named as a command line parameter: <lang Ursala>#import std

1. import int
1. executable -[parameterized]-

main = ~command.files.&h; <.contents:= %zP+ sum:-0+ %zp*FiNCS+ sep @L+ ~contents></lang> Creating a new file named after the input file with suffix .out: <lang Ursala>#import std

1. import int
1. executable -[parameterized]-

main =

~command.files.&h; ~&iNC+ file$[  contents: %zP+ sum:-0+ %zp*FiNCS+ sep @L+ ~contents, path: ~path; ^|C\~& ~=.-~; ^|T/~& '.out'!]</lang>  ## Vala Read from stdin while program running: <lang vala>stdout.printf("Please enter int value for A\n"); int a = int.parse(stdin.read_line()); stdout.printf("Please enter int value for B\n"); int b = int.parse(stdin.read_line()); stdout.printf("A+B = %d\n", a+b);</lang> ## VBScript <lang vb>Option Explicit Dim a, b Select Case WScript.Arguments.Count Case 0 'No arguments, prompt for them. WScript.Echo "Enter values for a and b" a = WScript.Stdin.ReadLine if Instr(a, " ") > 0 then 'If two variables were passed b = Split(a)(1) a = Split(a)(0) else WScript.Echo "Enter value for b" b = WScript.Stdin.ReadLine end if Case 1 'One argument, assume it's an input file, e.g. "in.txt" Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject") With FSO.OpenTextFile(WScript.Arguments(0), 1) a = .ReadLine b = Split(a)(1) a = Split(a)(0) .Close End With Case 2 'Two arguments, assume they are values a = WScript.Arguments(0) b = WScript.Arguments(1) End Select 'At this point, a and b are strings as entered, make them numbers a = CInt(a) b = CInt(b) 'Write the sum Wscript.Echo a + b if 1 = WScript.Arguments.Count then With FSO.CreateTextFile("out.txt") .WriteLine a + b .Close End With end if</lang> ## VHDL <lang VHDL>LIBRARY std; USE std.TEXTIO.all; entity test is end entity test; architecture beh of test is begin  process variable line_in, line_out : line; variable a,b : integer; begin readline(INPUT, line_in); read(line_in, a); read(line_in, b); write(line_out, a+b); writeline(OUTPUT, line_out); wait; -- needed to stop the execution end process;  end architecture beh;</lang> ## Visual Basic .NET <lang vbnet>Module Module1  Sub Main() Dim s() As String = Nothing   s = Console.ReadLine().Split(" "c) Console.WriteLine(CInt(s(0)) + CInt(s(1))) End Sub  End Module</lang> ## Whitespace <lang whitespace> </lang> ## X86 Assembly Works with: NASM version Linux <lang asm>section .text global _start _print: mov ebx, 1 mov eax, 4 int 0x80 ret _get_input: mov edx, 4 mov ebx, 0 mov eax, 3 int 0x80 ret _start: mov edx, in_val_len mov ecx, in_val_msg call _print mov ecx, a call _get_input ;make 'a' an actual number rather than a char. sub dword [a], 0x30 mov edx, in_val_len mov ecx, in_val_msg call _print mov ecx, b call _get_input ;calc real number for 'b' sub dword [b], 0x30 mov eax, dword [a] mov ebx, dword [b] add eax, ebx ;get the character for our sum. add eax, 0x30 mov dword [sum], eax mov edx, out_val_len mov ecx, out_val_msg call _print mov [sum+1], dword 0xa mov edx, 4 mov ecx, sum call _print push 0x1 mov eax, 1 push eax int 0x80 ret section .data in_val_msg db "Please input an integer:",0 in_val_len equ$-in_val_msg out_val_msg db "The sum of a+b is: ",0 out_val_len equ \$-out_val_msg

section .bss a resd 1 b resd 1 sum resd 1</lang> This will not work on numbers over 0(from 1 to 0). This is due to the fact, numbers higher than 0(10,11, etc) are in fact strings when taken from the keyboard. A much longer conversion code is required to loop through and treat each number in the string as separate numbers. For example, The number '10' would have to be treated as a 1 and a 0.

## XPL0

<lang XPL0>include c:\cxpl\codes; int A, B; [A:= IntIn(0);

B:= IntIn(0);
IntOut(0, A+B);
CrLf(0);


]</lang>

## Yorick

<lang yorick>a = b = 0; read, a, b; write, a + b;</lang>

## ZED

<lang ZED>(A+B) comment (always) (+) (read) (default-input-port)

   (read) (default-input-port)</lang>
`