Input/Output for pairs of numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added js-compatible version)
m (syntax highlighting fixup automation)
Line 23: Line 23:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F do_stuff(a, b)
<syntaxhighlight lang="11l">F do_stuff(a, b)
R a + b
R a + b


Line 29: Line 29:
L 1..t
L 1..t
V (a, b) = input().split(‘ ’).map(Int)
V (a, b) = input().split(‘ ’).map(Int)
print(do_stuff(a, b))</lang>
print(do_stuff(a, b))</syntaxhighlight>




=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>INT FUNC CalcSum(CHAR ARRAY s)
<syntaxhighlight lang="action!">INT FUNC CalcSum(CHAR ARRAY s)
INT sum,i
INT sum,i
CHAR ARRAY tmp(100)
CHAR ARRAY tmp(100)
Line 65: Line 65:
PrintIE(sums(i))
PrintIE(sums(i))
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_pairs_of_numbers.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_pairs_of_numbers.png Screenshot from Atari 8-bit computer]
Line 85: Line 85:
=={{header|Ada}}==
=={{header|Ada}}==
There can be newlines before or between numbers. The pairs may be on separate lines or the same line.
There can be newlines before or between numbers. The pairs may be on separate lines or the same line.
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;


Line 100: Line 100:
New_Line;
New_Line;
end loop;
end loop;
end Main;</lang>
end Main;</syntaxhighlight>
{{output}}
{{output}}
Output using the example input:
Output using the example input:
Line 113: Line 113:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Simple version - there can be newlines before or between the numbers
Simple version - there can be newlines before or between the numbers
<lang algol68># read a number from stand in then read and add that many pairs of numbers from stand in #
<syntaxhighlight 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 #
# 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
TO ( INT n; read( ( n, newline ) ); n ) DO
Line 120: Line 120:
print( ( a + b, newline ) )
print( ( a + b, newline ) )
OD
OD
</syntaxhighlight>
</lang>
Strict version - the pairs of numbers must appear on the same line.
Strict version - the pairs of numbers must appear on the same line.
<lang algol68>
<syntaxhighlight lang="algol68">
# read a number from stand in then read and add that many pairs of numbers from stand in #
# 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 #
# and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
Line 141: Line 141:
get( numbers, ( a, b ) );
get( numbers, ( a, b ) );
print( ( a + b, newline ) )
print( ( a + b, newline ) )
OD</lang>
OD</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 152: Line 152:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 GOSUB 230"INPUT LINE"
<syntaxhighlight lang="gwbasic"> 100 GOSUB 230"INPUT LINE"
110 LET N = VAL (L$) - 1
110 LET N = VAL (L$) - 1
120 IF N < 0 THEN END
120 IF N < 0 THEN END
Line 180: Line 180:
350 IF MID$(L$, C, 1) <> " " THEN NEXT C
350 IF MID$(L$, C, 1) <> " " THEN NEXT C
360 S = VAL(MID$(L$, 1, C - 1)) + VAL(MID$(L$, C + 1))
360 S = VAL(MID$(L$, 1, C - 1)) + VAL(MID$(L$, C + 1))
370 RETURN</lang>
370 RETURN</syntaxhighlight>
'''Input'''
'''Input'''
<pre>
<pre>
Line 199: Line 199:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>NR == 1 {n=$1; next}
<syntaxhighlight lang="awk">NR == 1 {n=$1; next}
NR > n+1 {exit}
NR > n+1 {exit}
{print $1+$2}</lang>
{print $1+$2}</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>
<syntaxhighlight lang="dos">
@echo off
@echo off
setlocal enabledelayedexpansion
setlocal enabledelayedexpansion
Line 219: Line 219:
for /l %%i in (1,1,%pairs%) do echo !sum%%i!
for /l %%i in (1,1,%pairs%) do echo !sum%%i!
pause>nul
pause>nul
</syntaxhighlight>
</lang>
{{in}}
{{in}}
<pre>
<pre>
Line 240: Line 240:
=={{header|BBC BASIC}}==
=={{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.
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.
<lang bbcbasic>INPUT n%
<syntaxhighlight lang="bbcbasic">INPUT n%
DIM pairs%(n% - 1, 1)
DIM pairs%(n% - 1, 1)
FOR i% = 0 TO n% - 1
FOR i% = 0 TO n% - 1
Line 249: Line 249:
FOR i% = 0 TO n% - 1
FOR i% = 0 TO n% - 1
PRINT pairs%(i%, 0) + pairs%(i%, 1)
PRINT pairs%(i%, 0) + pairs%(i%, 1)
NEXT</lang>
NEXT</syntaxhighlight>
With the sample inputs:
With the sample inputs:
<pre>?5
<pre>?5
Line 264: Line 264:


=={{header|C}}==
=={{header|C}}==
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 283: Line 283:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


Output for example input
Output for example input
Line 296: Line 296:


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using static System.Linq.Enumerable;
using static System.Linq.Enumerable;


Line 308: Line 308:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 320: Line 320:
=={{header|C++}}==
=={{header|C++}}==


<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>
using namespace std;
using namespace std;
Line 344: Line 344:
cout << doStuff(list[j].first, list[j].second) << endl;;
cout << doStuff(list[j].first, list[j].second) << endl;;
}
}
}</lang>
}</syntaxhighlight>


Run as per given input
Run as per given input
Line 365: Line 365:
=={{header|D}}==
=={{header|D}}==
This works with any number of integers on lines.
This works with any number of integers on lines.
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.conv, std.algorithm;
import std.stdio, std.string, std.conv, std.algorithm;


foreach (immutable _; 0 .. readln.strip.to!uint)
foreach (immutable _; 0 .. readln.strip.to!uint)
readln.split.to!(int[]).sum.writeln;
readln.split.to!(int[]).sum.writeln;
}</lang>
}</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>
<syntaxhighlight lang="factor">
USING: io math.parser prettyprint sequences splitting ;
USING: io math.parser prettyprint sequences splitting ;
IN: rosetta-code.pair-output
IN: rosetta-code.pair-output
Line 382: Line 382:
MAIN: main
MAIN: main
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 394: Line 394:
=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
{{works with|Fortran|95 and later}}
<lang fortran>program i_o_pairs
<syntaxhighlight lang="fortran">program i_o_pairs
implicit none
implicit none


Line 409: Line 409:
write(*, "(i0)") sum(pairs, 2)
write(*, "(i0)") sum(pairs, 2)


end program</lang>
end program</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Dim As UInteger n
Dim As UInteger n
Line 426: Line 426:
Print Str(sums(i))
Print Str(sums(i))
Next
Next
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 448: Line 448:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 470: Line 470:
fmt.Println(a + b)
fmt.Println(a + b)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{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.
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
<syntaxhighlight lang="haskell">main = do
contents <- getContents
contents <- getContents
let numberOfLines = read.head.lines$ contents
let numberOfLines = read.head.lines$ contents
nums = map (map read.words).take numberOfLines.tail.lines$ contents
nums = map (map read.words).take numberOfLines.tail.lines$ contents
sums = map sum nums
sums = map sum nums
mapM_ print sums</lang>
mapM_ print sums</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
$ cat <<EOF | jconsole -js '([: exit 0: [: smoutput [: ,. [: ({. {. }.) [: (+/"1) [: (0&".;._2) (1!:1)) 3'
$ cat <<EOF | jconsole -js '([: exit 0: [: smoutput [: ,. [: ({. {. }.) [: (+/"1) [: (0&".;._2) (1!:1)) 3'
> 5
> 5
Line 497: Line 497:
102
102
10
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.
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}}==
=={{header|Java}}==
<lang java>import java.util.Scanner;
<syntaxhighlight lang="java">import java.util.Scanner;


public class Main {
public class Main {
Line 521: Line 521:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 528: Line 528:


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.
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">
<lang sh>
< input.txt jq -n '
< input.txt jq -n '
input as $n
input as $n
Line 536: Line 536:
| [input,input] | add
| [input,input] | add
end'
end'
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 547: Line 547:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>parseints() = (a = split(strip(readline()), r"\s+"); map(x -> parse(Int, x), a))
<syntaxhighlight lang="julia">parseints() = (a = split(strip(readline()), r"\s+"); map(x -> parse(Int, x), a))
const lines = parseints()[1]
const lines = parseints()[1]
Line 554: Line 554:
println(sum(parseints()))
println(sum(parseints()))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
3
3
Line 566: Line 566:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


import java.util.Scanner
import java.util.Scanner
Line 581: Line 581:
println()
println()
for (i in 0 until n) println(x[i] + y[i])
for (i in 0 until n) println(x[i] + y[i])
}</lang>
}</syntaxhighlight>
Sample input/output:
Sample input/output:
{{out}}
{{out}}
Line 601: Line 601:
=={{header|Lua}}==
=={{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.
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.
<lang Lua>local intTab, numLines, sum = {}, io.read()
<syntaxhighlight lang="lua">local intTab, numLines, sum = {}, io.read()
for i = 1, numLines do
for i = 1, numLines do
sum = 0
sum = 0
Line 607: Line 607:
table.insert(intTab, sum)
table.insert(intTab, sum)
end
end
for _, result in pairs(intTab) do print(result) end</lang>
for _, result in pairs(intTab) do print(result) end</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import sequtils, strutils
<syntaxhighlight lang="nim">import sequtils, strutils


let lineCount = stdin.readLine.parseInt()
let lineCount = stdin.readLine.parseInt()
Line 618: Line 618:
assert fields.len == 2
assert fields.len == 2
let pair = fields.map(parseInt)
let pair = fields.map(parseInt)
echo pair[0] + pair[1]</lang>
echo pair[0] + pair[1]</syntaxhighlight>


{{Out}}
{{Out}}
Line 630: Line 630:
=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let () =
<syntaxhighlight lang="ocaml">let () =
let n = int_of_string (input_line stdin) in
let n = int_of_string (input_line stdin) in
for i = 1 to n do
for i = 1 to n do
Line 641: Line 641:
| _ ->
| _ ->
raise (Invalid_argument "wrong input")
raise (Invalid_argument "wrong input")
done</lang>
done</syntaxhighlight>


{{out}}
{{out}}
Line 663: Line 663:


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:
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:
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <pari/pari.h>
#include <pari/pari.h>
Line 696: Line 696:
pari_printf("%Ps", f);
pari_printf("%Ps", f);
return 0;
return 0;
}</lang>
}</syntaxhighlight>
Of course for such a simple task this has very little advantage over C, but it does demonstrate the general principle.
Of course for such a simple task this has very little advantage over C, but it does demonstrate the general principle.


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>program inputOutputForPairsOfNumbers(input, output);
<syntaxhighlight lang="pascal">program inputOutputForPairsOfNumbers(input, output);
var
var
lines: integer;
lines: integer;
Line 712: Line 712:
writeLn(x + y)
writeLn(x + y)
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
3
3
Line 722: Line 722:
=={{header|Perl}}==
=={{header|Perl}}==
Reads from STDIN, added any pair of numbers.
Reads from STDIN, added any pair of numbers.
<lang perl>$n = scalar <>;
<syntaxhighlight lang="perl">$n = scalar <>;


for (1..$n) {
for (1..$n) {
($a,$b) = split ' ', <>;
($a,$b) = split ' ', <>;
print $a + $b . "\n";
print $a + $b . "\n";
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<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>
<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>
Line 749: Line 749:
<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: #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: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
(or more accurately the final state of the console)
(or more accurately the final state of the console)
Line 765: Line 765:
And hence runnable in a browser, as well as on the desktop.<br>
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]].
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]].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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;">"""
<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;">"""
Line 789: Line 789:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Output same as the last line of the above.
Output same as the last line of the above.


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
# script.ps1
# script.ps1


Line 805: Line 805:


# ./script file.txt
# ./script file.txt
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
<lang python>def do_stuff(a, b):
<syntaxhighlight lang="python">def do_stuff(a, b):
return a + b
return a + b


Line 814: Line 814:
for x in range(0, t):
for x in range(0, t):
a, b = raw_input().strip().split()
a, b = raw_input().strip().split()
print do_stuff(int(a), int(b))</lang>
print do_stuff(int(a), int(b))</syntaxhighlight>


===Python: Alternative===
===Python: Alternative===
Or without the function do_stuff() and that works for Python 3 and Python 2:
Or without the function do_stuff() and that works for Python 3 and Python 2:
<lang python>>>> try: raw_input
<syntaxhighlight lang="python">>>> try: raw_input
except NameError: raw_input = input
except NameError: raw_input = input


Line 838: Line 838:
5 5
5 5
10
10
>>> </lang>
>>> </syntaxhighlight>
(All but the first line of single numbers, (the 5), is output from the program).
(All but the first line of single numbers, (the 5), is output from the program).


===Python: With prompts===
===Python: With prompts===
More than is asked for by the task, but if working interactively then the following version adds prompts.
More than is asked for by the task, but if working interactively then the following version adds prompts.
<lang python>>>> for i in range(int(raw_input('lines: '))):
<syntaxhighlight lang="python">>>> for i in range(int(raw_input('lines: '))):
print(sum(int(numberstring)
print(sum(int(numberstring)
for numberstring in raw_input('two numbers: ').strip().split()))
for numberstring in raw_input('two numbers: ').strip().split()))
Line 859: Line 859:
two numbers: 5 5
two numbers: 5 5
10
10
>>> </lang>
>>> </syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
;(define line-number (read)) ;reads all kind of things
;(define line-number (read)) ;reads all kind of things
;(for ([i (in-range line-number)])
;(for ([i (in-range line-number)])
Line 873: Line 873:
(displayln (apply +
(displayln (apply +
(map string->number
(map string->number
(string-split (read-line))))))</lang>
(string-split (read-line))))))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>for ^get() { say [+] get.words }</lang>
<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.
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}}==
=={{header|REXX}}==
This version isn't limited to summing integers, any form of number that REXX supports can be used.
This version isn't limited to summing integers, any form of number that REXX supports can be used.
<lang rexx>/*REXX pgm reads a number (from the CL), reads that number of pairs, & writes their sum.*/
<syntaxhighlight 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. */
/*all input is from the Command Line. */
do linein() /*read the number of pairs to be add*ed*/
do linein() /*read the number of pairs to be add*ed*/
Line 888: Line 888:
say word($, 1) + word($, 2) /*display the sum of a pair of numbers.*/
say word($, 1) + word($, 2) /*display the sum of a pair of numbers.*/
end /*linein() */
end /*linein() */
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Input/Output for Pairs of Numbers
# Project : Input/Output for Pairs of Numbers


Line 909: Line 909:
ok
ok
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 920: Line 920:


===Ring: Alternative===
===Ring: Alternative===
<lang ring>
<syntaxhighlight lang="ring">
# Project : Input/Output for Pairs of Numbers (Alternative)
# Project : Input/Output for Pairs of Numbers (Alternative)


Line 937: Line 937:
ok
ok
next
next
>>> </lang>
>>> </syntaxhighlight>
<pre>
<pre>
3
3
Line 947: Line 947:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>n = gets.to_i
<syntaxhighlight lang="ruby">n = gets.to_i
n.times do
n.times do
a, b = gets.split.map(&:to_i)
a, b = gets.split.map(&:to_i)
puts a + b
puts a + b
end</lang>
end</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object IOPairs extends App {
<syntaxhighlight lang="scala">object IOPairs extends App {
private val in = scala.io.StdIn
private val in = scala.io.StdIn
private val n = in.readInt()
private val n = in.readInt()
Line 967: Line 967:


}
}
</syntaxhighlight>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>gets stdin n
<syntaxhighlight lang="tcl">gets stdin n
while {$n > 0} {
while {$n > 0} {
if {[scan [gets stdin] "%d %d" a b] == 2} {
if {[scan [gets stdin] "%d %d" a b] == 2} {
Line 976: Line 976:
}
}
incr n -1
incr n -1
}</lang>
}</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 982: Line 982:
{{works with|Bourne Again SHell}}
{{works with|Bourne Again SHell}}


<lang bash>read n
<syntaxhighlight lang="bash">read n
while (( n > 0 )); do
while (( n > 0 )); do
read a b
read a b
echo $((a+b))
echo $((a+b))
((n--))
((n--))
done</lang>
done</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
<lang ursa>decl int amount
<syntaxhighlight lang="ursa">decl int amount
set amount (in int console)
set amount (in int console)


Line 1,004: Line 1,004:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl console
out (int (+ ints<i> ints<(int (+ i 1))>)) endl console
end for</lang>
end for</syntaxhighlight>
Networked version. Runs on port 20000.
Networked version. Runs on port 20000.
<lang ursa>decl serverport sp
<syntaxhighlight lang="ursa">decl serverport sp
decl port p
decl port p
sp.attach 20000
sp.attach 20000
Line 1,025: Line 1,025:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl p
out (int (+ ints<i> ints<(int (+ i 1))>)) endl p
end for</lang>
end for</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
This assumes that both Stdin and Stdout are connected to a terminal.
This assumes that both Stdin and Stdout are connected to a terminal.
<lang ecmascript>import "io" for Stdin
<syntaxhighlight lang="ecmascript">import "io" for Stdin


var output = Fn.new { |pairs| pairs.each { |p| System.print(p[0] + p[1]) } }
var output = Fn.new { |pairs| pairs.each { |p| System.print(p[0] + p[1]) } }
Line 1,047: Line 1,047:
}
}
System.print()
System.print()
output.call(pairs)</lang>
output.call(pairs)</syntaxhighlight>


{{out}}
{{out}}
Line 1,068: Line 1,068:
=={{header|XPL0}}==
=={{header|XPL0}}==
The input file must be redirected on the command line, for example: iopair <iopair.txt
The input file must be redirected on the command line, for example: iopair <iopair.txt
<lang XPL0>int N;
<syntaxhighlight lang="xpl0">int N;
for N:= 1 to IntIn(1) do
for N:= 1 to IntIn(1) do
[IntOut(0, IntIn(1) + IntIn(1));
[IntOut(0, IntIn(1) + IntIn(1));
CrLf(0);
CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 1,085: Line 1,085:
=={{header|zkl}}==
=={{header|zkl}}==
Using the console as the input stream:
Using the console as the input stream:
<lang zkl>fcn pairs{
<syntaxhighlight lang="zkl">fcn pairs{
n:=ask("num pairs: ").toInt();
n:=ask("num pairs: ").toInt();
do(n){ask("1 pair: ").split(" ").sum().println()}
do(n){ask("1 pair: ").split(" ").sum().println()}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>