Input/Output for pairs of numbers: Difference between revisions
(Created page with "Basic example to take in input of number pairs from STDIN and output sum of each pair to STDOUT. == Input/Output == '''Input''' <lang>5 1 2 10 20 -3 5 100 2 5 5</lang> ''...") |
(formatted as task.) |
||
Line 1: | Line 1: | ||
{{draft task}} |
|||
Basic example to take in input of number pairs from STDIN and output sum of each pair to STDOUT. |
|||
From lines of input starting with a line containing the numbers of pairs to follows, followed by that number of pairs of integers separated by a space on separate lines from STDIN, output the sum of each pair to STDOUT. |
|||
;Sample input with corresponding output: |
|||
== Input/Output == |
|||
'''Input''' |
'''Input''' |
||
< |
<pre>5 |
||
1 2 |
1 2 |
||
10 20 |
10 20 |
||
-3 5 |
-3 5 |
||
100 2 |
100 2 |
||
5 5</ |
5 5</pre> |
||
'''Output''' |
'''Output''' |
||
< |
<pre>3 |
||
30 |
30 |
||
2 |
2 |
||
102 |
102 |
||
10</ |
10</pre> |
||
== |
=={{header|Java}}== |
||
<lang java>import java.util.Scanner; |
<lang java>import java.util.Scanner; |
||
Line 66: | Line 68: | ||
== |
=={{header|Python}}== |
||
<lang python>def do_stuff(a, b): |
<lang python>def do_stuff(a, b): |
||
return a + b |
return a + b |
||
Line 76: | Line 78: | ||
== |
=={{header|Ruby}}== |
||
<lang ruby>def do_stuff(a, b) |
<lang ruby>def do_stuff(a, b) |
Revision as of 07:44, 4 December 2013
From lines of input starting with a line containing the numbers of pairs to follows, followed by that number of pairs of integers separated by a space on separate lines from STDIN, output the sum of each pair to STDOUT.
- Sample input with corresponding output
Input
5 1 2 10 20 -3 5 100 2 5 5
Output
3 30 2 102 10
Java
<lang java>import java.util.Scanner;
public class Main {
public static int doStuff(int a, int b){ int sum = a+b; return sum; }
public static void main(String[] args) { Scanner in = new Scanner(System.in);
int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); int result = doStuff(a, b); System.out.println(result); } } }</lang>
C++
<lang c++>#include <iostream> using namespace std;
int doStuff(int a, int b) {
return a + b;
}
int main() { int t; cin >> t; for(int j=0; j<t; j++){ int a; int b; cin >> a; cin >> b; cout << doStuff(a, b) << endl;; } return 0; }</lang>
Python
<lang python>def do_stuff(a, b): return a + b
t = input() for x in range(0, t): a, b = raw_input().strip().split() print do_stuff(int(a), int(b))</lang>
Ruby
<lang ruby>def do_stuff(a, b) a + b end
t = gets.to_i for i in 1..t do a, b = gets.strip.split.map {|i| i.to_i} puts do_stuff(a, b) end</lang>