Input/Output for pairs of numbers: Difference between revisions

Added Easylang
(Added Easylang)
 
(36 intermediate revisions by 22 users not shown)
Line 19:
102
10</pre>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F do_stuff(a, b)
R a + b
 
V t = Int(input())
L 1..t
V (a, b) = input().split(‘ ’).map(Int)
print(do_stuff(a, b))</syntaxhighlight>
 
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC CalcSum(CHAR ARRAY s)
INT sum,i
CHAR ARRAY tmp(100)
 
sum=ValI(s)
FOR i=1 TO s(0)
DO
IF s(i)=32 THEN EXIT FI
OD
SCopyS(tmp,s,i,s(0))
sum==+ValI(tmp)
RETURN (sum)
 
PROC Main()
BYTE i,nLines
INT ARRAY sums(256)
CHAR ARRAY line(256)
 
nLines=InputB()
IF nLines=0 THEN RETURN FI
 
FOR i=0 TO nLines-1
DO
InputS(line)
sums(i)=CalcSum(line)
OD
 
PutE()
FOR i=0 TO nLines-1
DO
PrintIE(sums(i))
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_pairs_of_numbers.png Screenshot from Atari 8-bit computer]
<pre>
5
1 2
10 20
-3 5
100 2
5 5
 
3
30
2
102
10
</pre>
 
=={{header|Ada}}==
There can be newlines before or between numbers. The pairs may be on separate lines or the same line.
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
 
procedure Main is
count : Integer;
First : Integer;
Second : Integer;
begin
Get (count);
for I in 1 .. count loop
Get (First);
Get (Second);
Put (Item => First + Second, Width => 1);
New_Line;
end loop;
end Main;</syntaxhighlight>
{{output}}
Output using the example input:
<pre>
3
30
2
102
10
</pre>
 
=={{header|ALGOL 68}}==
Simple version - there can be newlines before or between the numbers
<langsyntaxhighlight lang="algol68"># read a number from stand in then read and add that many pairs of numbers from stand in #
# and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
TO ( INT n; read( ( n, newline ) ); n ) DO
Line 29 ⟶ 120:
print( ( a + b, newline ) )
OD
</syntaxhighlight>
</lang>
Strict version - the pairs of numbers must appear on the same line.
<langsyntaxhighlight lang="algol68">
# read a number from stand in then read and add that many pairs of numbers from stand in #
# and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
Line 50 ⟶ 141:
get( numbers, ( a, b ) );
print( ( a + b, newline ) )
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 59 ⟶ 150:
+10
</pre>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 GOSUB 230"INPUT LINE"
110 LET N = VAL (L$) - 1
120 IF N < 0 THEN END
130 DIM SUM(N)
140 FOR I = 0 TO N
150 GOSUB 330"SUM PAIR FROM INPUT LINE"
160 LET SUM(I) = S
170 NEXT I
190 FOR I = 0 TO N
200 PRINT SUM(I)
210 NEXT
220 END
 
230 LET L$ = ""
240 LET C$ = ""
250 FOR C = 0 TO 1 STEP 0
260 LET L$ = L$ + C$
270 GET C$
280 PRINT CHR$ (0)C$;
290 LET C = C$ = CHR$ (13)
300 NEXT C
310 LET C = FRE (0)
320 RETURN
 
330 GOSUB 230"INPUT LINE"
340 FOR C = 1 TO LEN(L$)
350 IF MID$(L$, C, 1) <> " " THEN NEXT C
360 S = VAL(MID$(L$, 1, C - 1)) + VAL(MID$(L$, C + 1))
370 RETURN</syntaxhighlight>
'''Input'''
<pre>
5
1 2
10 20
-3 5
100 2
5 5
</pre>
{{out}}
<pre>
3
30
2
102
10
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">printNumbers: function [num]->
print [num\0 "+" num\1 "=" num\0 + num\1]
 
lineCount: to :integer strip input ""
 
do.times:lineCount [
numbers: to [:integer] split.words input ""
printNumbers numbers
]</syntaxhighlight>
 
{{out}}
 
<pre>3
2 10
2 + 10 = 12
4 5
4 + 5 = 9
-123 45
-123 + 45 = -78</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">NR == 1 {n=$1; next}
NR > n+1 {exit}
{print $1+$2}</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 81 ⟶ 241:
for /l %%i in (1,1,%pairs%) do echo !sum%%i!
pause>nul
</syntaxhighlight>
</lang>
{{in}}
<pre>
Line 102 ⟶ 262:
=={{header|BBC BASIC}}==
The specification is a bit ambiguous, but I understood it as wanting us to read all the numbers in <i>first</i> and then print all the sums. This program does that. It could be a couple of lines shorter if we were allowed to use a comma instead of a space as separator.
<langsyntaxhighlight lang="bbcbasic">INPUT n%
DIM pairs%(n% - 1, 1)
FOR i% = 0 TO n% - 1
Line 111 ⟶ 271:
FOR i% = 0 TO n% - 1
PRINT pairs%(i%, 0) + pairs%(i%, 1)
NEXT</langsyntaxhighlight>
With the sample inputs:
<pre>?5
Line 126 ⟶ 286:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 145 ⟶ 305:
 
return 0;
}</langsyntaxhighlight>
 
Output for example input
 
<pre>
3
30
2
102
10
</pre>
 
=={{header|C sharp}}==
<syntaxhighlight lang="csharp">using System;
using static System.Linq.Enumerable;
 
public class Program
{
static void Main(string[] args)
{
int count = Convert.ToInt32(Console.ReadLine());
for (int line = 0; line < count; line++) {
Console.WriteLine(Console.ReadLine().Split(' ').Sum(i => Convert.ToInt32(i)));
}
}
}</syntaxhighlight>
{{out}}
<pre>
3
Line 158 ⟶ 341:
 
=={{header|C++}}==
 
Modified in order to take in all inputs and then give the output, the original gave the output for each pair immediately.
<syntaxhighlight lang="cpp">#include <iostream>
<lang cpp>
#include <vector>
/*Modified by Abhishek Ghosh, 19th March 2014, Rotterdam*/
#include <iostream>
using namespace std;
int doStuff(int a, int b) {
return a + b;
}
int main() {
int t, **list;
cin >> t;
list = newvector<pair<int, int*[>> list(t]);
for(int j=0; j<t; j++){
cin >> list[j].first >> list[j].second;
list[j] = new int[2];
cin >> list[j][0]>> list[j][1];
}
cout << endl;
for(int j=0;j<t;j++){
cout << doStuff(list[j][0].first, list[j][1].second) << endl;;
}
}</syntaxhighlight>
return 0;
}
</lang>
 
Run as per given input
Line 208 ⟶ 384:
10
</pre>
 
 
=={{header|D}}==
This works with any number of integers on lines.
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.conv, std.algorithm;
 
foreach (immutable _; 0 .. readln.strip.to!uint)
readln.split.to!(int[]).sum.writeln;
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
n = number input
for i to n
a[] = number strsplit input " "
print a[1] + a[2]
.
input_data
5
1 2
10 20
-3 5
100 2
5 5
</syntaxhighlight>
{{out}}
<pre>
3
30
2
102
10
</pre>
 
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">
USING: io math.parser prettyprint sequences splitting ;
IN: rosetta-code.pair-output
 
: process-line ( str -- n )
" " split [ string>number ] map-sum ;
: main ( -- ) lines 1 tail [ process-line ] map [ . ] each ;
MAIN: main
</syntaxhighlight>
{{out}}
<pre>
3
30
2
102
10
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program i_o_pairs
implicit none
 
Line 236 ⟶ 456:
write(*, "(i0)") sum(pairs, 2)
 
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim As UInteger n
Line 253 ⟶ 473:
Print Str(sums(i))
Next
Sleep</langsyntaxhighlight>
 
{{out}}
Line 271 ⟶ 491:
</pre>
 
=={{header|HaskellFree Pascal}}==
''See [[#Pascal|Pascal]]''
This solution will actually add any number of integers placed on each line. Additionally, after removing the bits of code that cut out the specified number of lines, the solution will sum any number of lines of integers.
 
<lang Haskell>main = do
contents <- getContents
let numberOfLines = read.head.lines$ contents
nums = map (map read.words).take numberOfLines.tail.lines$ contents
sums = map sum nums
mapM_ print sums</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 304 ⟶ 517:
fmt.Println(a + b)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
This solution will actually add any number of integers placed on each line. Additionally, after removing the bits of code that cut out the specified number of lines, the solution will sum any number of lines of integers.
 
<syntaxhighlight lang="haskell">main = do
contents <- getContents
let numberOfLines = read.head.lines$ contents
nums = map (map read.words).take numberOfLines.tail.lines$ contents
sums = map sum nums
mapM_ print sums</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
$ cat <<EOF | jconsole -js '([: exit 0: [: smoutput [: ,. [: ({. {. }.) [: (+/"1) [: (0&".;._2) (1!:1)) 3'
> 5
Line 321 ⟶ 544:
102
10
</syntaxhighlight>
</lang>
Considerably simpler than [[http://rosettacode.org/wiki/Input/Output_for_Lines_of_Text#J|see explanation]] output for lines of text, this sentence is a single fork. J pads the numeric arrays of 0&".;._2 (numbers cut) with 0 . We form the +/"1 (row sums), then take the sum of the first row of the beheaded sums ({. {. }.) for display. ,. (raveled items) reshapes the vector into a column-vector shaped matrix. And the [: (cap) causes the monadic form of the verb to cap's right.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Scanner;
 
public class Main {
Line 345 ⟶ 568:
}
}
}</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The solution below assumes the input is in a file named input.txt, and is quite lenient about the presentation of the numbers. For example, it does not require that each pair of numbers be presented on the same line.
<syntaxhighlight lang="sh">
< input.txt jq -n '
input as $n
| if $n | (type != "number" or . < 0)
then "Number of pairs must be non-negative." | error
else range(0; $n)
| [input,input] | add
end'
</syntaxhighlight>
{{out}}
<pre>
3
30
2
102
10
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">parseints() = (a = split(strip(readline()), r"\s+"); map(x -> parse(Int, x), a))
const lines = parseints()[1]
 
for _ in 1:lines
println(sum(parseints()))
end
</syntaxhighlight>{{out}}
<pre>
3
5 6
11
8 2
10
9 23
32
</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.util.Scanner
Line 363 ⟶ 628:
println()
for (i in 0 until n) println(x[i] + y[i])
}</langsyntaxhighlight>
Sample input/output:
{{out}}
Line 383 ⟶ 648:
=={{header|Lua}}==
This solution will sum any number of space-separated numbers per input line, assuming the user won't input too many to store in the available RAM.
<langsyntaxhighlight Lualang="lua">local intTab, numLines, sum = {}, io.read()
for i = 1, numLines do
sum = 0
Line 389 ⟶ 654:
table.insert(intTab, sum)
end
for _, result in pairs(intTab) do print(result) end</langsyntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import sequtils, strutils
 
let lineCount = stdin.readLine.parseInt()
for _ in 1..lineCount:
let line = stdin.readLine()
let fields = line.splitWhitespace()
assert fields.len == 2
let pair = fields.map(parseInt)
echo pair[0] + pair[1]</syntaxhighlight>
 
{{Out}}
For the sample input:
<pre>3
30
2
102
10</pre>
 
=={{header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let () =
let n = int_of_string (input_line stdin) in
for i = 1 to n do
let line = input_line stdin in
match String.split_on_char ' ' line with
| a::b::[] ->
let x = int_of_string a + int_of_string b in
print_int x;
print_newline ()
| _ ->
raise (Invalid_argument "wrong input")
done</syntaxhighlight>
 
{{out}}
<pre>
$ cat input.txt
5
1 2
10 20
-3 5
100 2
5 5
$ cat input.txt | ocaml pairs.ml
3
30
2
102
10
</pre>
 
=={{header|PARI/GP}}==
 
Interestingly, this task is not possible to implement directly in GP, since <code>input()</code>, like the gp REPL itself, ignores spaces. One must use PARI:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <pari/pari.h>
Line 427 ⟶ 743:
pari_printf("%Ps", f);
return 0;
}</langsyntaxhighlight>
Of course for such a simple task this has very little advantage over C, but it does demonstrate the general principle.
 
=={{header|Perl 6Pascal}}==
<syntaxhighlight lang="pascal">program inputOutputForPairsOfNumbers(input, output);
<lang perl6>for ^get() { say [+] get.words }</lang>
var
This does more than the task asks. It will sum as many numbers as you care to put on each line, and the numbers need not be integers, but may also be a mix of rational, floating-point, or complex numbers. More subtly, <tt>get</tt> can read from a file specified as a command-line argument, but defaults to taking STDIN if no filename is specified.
lines: integer;
x: integer;
y: integer;
begin
readLn(lines);
for lines := 1 to lines do
begin
readLn(x, y);
writeLn(x + y)
end
end.</syntaxhighlight>
{{out}}
3
30
2
102
10
 
=={{header|Perl}}==
Reads from STDIN, added any pair of numbers.
<syntaxhighlight lang="perl">$n = scalar <>;
 
for (1..$n) {
($a,$b) = split ' ', <>;
print $a + $b . "\n";
}</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>string line = gets(0)
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
sequence r = scanf(trim(line),"%d"), s = {}
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
if length(r)!=1 then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
puts(1,"input not a number\n")
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
abort(0)
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a number"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
puts(1,"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
for i=1 to r[1][1] do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
line = gets(0)
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
r = scanf(trim(line),"%d %d")
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d %d"</span><span style="color: #0000FF;">)</span>
if length(r)!=1 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
puts(1,"input not a pair of numbers\n")
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a pair of numbers"</span><span style="color: #0000FF;">)</span>
abort(0)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
s &= sum(r[1])
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
puts(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"===\n"</span><span style="color: #0000FF;">)</span>
puts(1,"===\n")
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
?s</lang>
<!--</syntaxhighlight>-->
{{out}}
(or more accurately the final state of the console)
Line 466 ⟶ 809:
{3,30,2,102,10}
</pre>
===avoiding file i/o===
And hence runnable in a browser, as well as on the desktop.<br>
User input would need to be a proper GUI rather than a console prompt, perhaps like [[Arithmetic/Integer#Phix]] or the much prettier/more polished [[Morse_code#Phix]].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"""
5
1 2
10 20
-3 5
100 2
5 5"""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a number"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d %d"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a pair of numbers"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Output same as the last line of the above.
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
# script.ps1
 
Line 480 ⟶ 852:
 
# ./script file.txt
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def do_stuff(a, b):
return a + b
 
Line 489 ⟶ 861:
for x in range(0, t):
a, b = raw_input().strip().split()
print do_stuff(int(a), int(b))</langsyntaxhighlight>
 
===Python: Alternative===
Or without the function do_stuff() and that works for Python 3 and Python 2:
<langsyntaxhighlight lang="python">>>> try: raw_input
except NameError: raw_input = input
 
Line 513 ⟶ 885:
5 5
10
>>> </langsyntaxhighlight>
(All but the first line of single numbers, (the 5), is output from the program).
 
===Python: With prompts===
More than is asked for by the task, but if working interactively then the following version adds prompts.
<langsyntaxhighlight lang="python">>>> for i in range(int(raw_input('lines: '))):
print(sum(int(numberstring)
for numberstring in raw_input('two numbers: ').strip().split()))
Line 534 ⟶ 906:
two numbers: 5 5
10
>>> </langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> []
$ "How many pairs? " input
quackery times
[ $ "Pair "
i^ 1+ number$ join
$ ": " join input
join
$ " + echo cr " join ]
quackery</syntaxhighlight>
 
{{out}}
 
<pre>How many pairs? 5
Pair 1: 1 2
Pair 2: 10 20
Pair 3: -3 5
Pair 4: 100 2
Pair 5: 5 5
3
30
2
102
10
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
;(define line-number (read)) ;reads all kind of things
;(for ([i (in-range line-number)])
Line 548 ⟶ 947:
(displayln (apply +
(map string->number
(string-split (read-line))))))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>for ^get() { say [+] get.words }</syntaxhighlight>
This does more than the task asks. It will sum as many numbers as you care to put on each line, and the numbers need not be integers, but may also be a mix of rational, floating-point, or complex numbers. More subtly, <tt>get</tt> can read from a file specified as a command-line argument, but defaults to taking STDIN if no filename is specified.
 
=={{header|REXX}}==
This version isn't limited to summing integers, any form of number that REXX supports can be used.
<langsyntaxhighlight lang="rexx">/*REXX pgm reads a number (from the CL), reads that number of pairs, & writes their sum.*/
/*all input is from the Command Line. */
do linein() /*read the number of pairs to be add*ed*/
Line 558 ⟶ 962:
say word($, 1) + word($, 2) /*display the sum of a pair of numbers.*/
end /*linein() */
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Input/Output for Pairs of Numbers
# Date : 2017/10/06
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
pairs = ["5", "1 2", "10 20", "-3 5", "100 2", "5 5"]
Line 582 ⟶ 983:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 593 ⟶ 994:
 
===Ring: Alternative===
<langsyntaxhighlight lang="ring>>>">
# Project : Input/Output for Pairs of Numbers (Alternative)
# Date : 2017/10/06
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
 
pairs = ["5", "1 2", "10 20", "5 -3", "100 2", "5 5"]
Line 613 ⟶ 1,011:
ok
next
>>> </langsyntaxhighlight>
<pre>
3
Line 620 ⟶ 1,018:
102
10
</pre>
 
=={{header|RPL}}==
« 1 "How many pairs" "" INPUT STR→ '''FOR''' j
"Enter pair #" j + "" INPUT STR→ +
'''NEXT'''
» '<span style=color:blue">TASK</span>' STO
{{out}}
<pre>
5: 3
4: 30
3: 2
2: 102
1: 10
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">n = gets.to_i
n.times do
a, b = gets.split.map(&:to_i)
puts a + b
end</langsyntaxhighlight>
 
=={{header|TclScala}}==
<syntaxhighlight lang="scala">object IOPairs extends App {
private val in = scala.io.StdIn
private val n = in.readInt()
 
for (_ <- 0 until n) {
<lang tcl>gets stdin n
val Array(a, b) = in.readLine().split(" ").map(_.toInt)
 
def doStuff(a: Long, b: Long): Long = a + b
 
println(doStuff(a, b))
}
 
}
</syntaxhighlight>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">gets stdin n
while {$n > 0} {
if {[scan [gets stdin] "%d %d" a b] == 2} {
Line 637 ⟶ 1,064:
}
incr n -1
}</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 643 ⟶ 1,070:
{{works with|Bourne Again SHell}}
 
<langsyntaxhighlight lang="bash">read n
while (( n > 0 )); do
read a b
echo $((a+b))
((n--))
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl int amount
set amount (in int console)
 
Line 665 ⟶ 1,092:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl console
end for</langsyntaxhighlight>
Networked version. Runs on port 20000.
<langsyntaxhighlight lang="ursa">decl serverport sp
decl port p
sp.attach 20000
Line 686 ⟶ 1,113:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl p
end for</langsyntaxhighlight>
 
=={{header|Wren}}==
This assumes that both Stdin and Stdout are connected to a terminal.
<syntaxhighlight lang="wren">import "io" for Stdin
 
var output = Fn.new { |pairs| pairs.each { |p| System.print(p[0] + p[1]) } }
 
var n = Num.fromString(Stdin.readLine())
if (!n || !n.isInteger || n < 1) Fiber.abort("Number of pairs must be a positive integer.")
var pairs = []
for (i in 0...n) {
var line = Stdin.readLine()
var sp = line.split(" ")
if (sp.count != 2) Fiber.abort("Each line must contain 2 integers, separated by a space.")
var p1 = Num.fromString(sp[0])
if (!p1 || !p1.isInteger) Fiber.abort("First value is not an integer.")
var p2 = Num.fromString(sp[1])
if (!p2 || !p2.isInteger) Fiber.abort("Second value is not an integer.")
pairs.add([p1, p2])
}
System.print()
output.call(pairs)</syntaxhighlight>
 
{{out}}
Sample input/output:
<pre>
5
1 2
10 20
-3 5
100 2
5 5
 
3
30
2
102
10
</pre>
 
=={{header|XPL0}}==
The input file must be redirected on the command line, for example: iopair <iopair.txt
<syntaxhighlight lang="xpl0">int N;
for N:= 1 to IntIn(1) do
[IntOut(0, IntIn(1) + IntIn(1));
CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
3
30
2
102
10
</pre>
 
=={{header|zkl}}==
Using the console as the input stream:
<langsyntaxhighlight lang="zkl">fcn pairs{
n:=ask("num pairs: ").toInt();
do(n){ask("1 pair: ").split(" ").sum().println()}
}</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits