Hide
In this article
This is best explained with a simple problem statement and solutions in different languages.
Problem Statement
Write a program that adds two numbers prints the sum to STDOUT. Read the input from STDIN. The first line of your input will contain an integer (N) that tells you how many more lines there are in the input. Each of the subsequent N lines contains 2 integers). You need to print the sum of each pair on a separate line of STDOUT.
Sample Input
3 1 5 3 10 999 -34343
Sample Output
6 13 -33344
Solution
C
6 #include int main () { int n, i, a, b; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d %d", &a, &b); printf("%d\n", a+b); } }
Solution
C++
6 #include using namespace std; int main() { int n; cin >> n; for(int i = 0; i < n; i++) { int a, b; cin >> a >> b; cout << a+b << "\n"; } return 0; }
Solution
C#
using System; using System.IO; class Solution { static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); for(int t = 0; t < n; t++) { String str = Console.ReadLine(); String[] strArr = str.Split(); int a = Convert.ToInt32(strArr[0]); int b = Convert.ToInt32(strArr[1]); Console.WriteLine(a+b); } } }
Solution
Java
import java.io.*; import java.util.*; class Solution { public static void main(String [] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int t = 0; t < n; t++) { int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a+b); } } }
Solution
Objective C
#import<Foundation/Foundation.h> int main(int argc, const char* argv[]) { @autoreleasepool { NSInteger t; scanf("%lu", &t); for (NSInteger i = 0; i < t; i++) { NSInteger a, b; scanf("%lu %lu", &a, &b); /* * Do not use NSLog to print to stdout */ printf("%lu\n", a + b); } return 0; } }
Solution
Python
n = int(raw_input()) for i in range(0,n): a, b = raw_input().split() print int(a) + int(b)
Solution
Python 3
n = int(input()) for i in range(n): a, b = input().strip().split(' ') print (int(a) + int(b))
Solution
Ruby
n = gets.to_i (0...n).each do |i| lis = gets.strip.split(" ") a = lis[0].to_i b = lis[1].to_i puts a + b end
Solution
PHP
<!?php $handle = fopen ("php://stdin","r"); $t = fgets($handle); for($i=0; $i<$t; $i++) { $lis = explode(" ", fgets($handle)); print((int)$lis[0] + (int)$lis[1]) . "\n"; } fclose($handle); ?>
Solution
Javascript
process.stdin.resume(); process.stdin.setEncoding('ascii'); var __input_stdin = ""; var __input_stdin_array = ""; var __input_currentline = 0; process.stdin.on('data', function (data) { __input_stdin += data; }); process.stdin.on('end', function () { __input_stdin_array = __input_stdin.split("\n"); var res; var n = parseInt(__input_stdin_array[__input_currentline].trim(), 10); __input_currentline += 1; for (var i = 0; i<n;i++) { var _line = __input_stdin_array[__input_currentline].trim(); __input_currentline += 1; var line = _line.split(" "); var _a = parseInt(line[0]); var _b = parseInt(line[1]); res = _a + _b; process.stdout.write(""+res+"\n"); } });
Solution
Clojure
(use '[clojure.string :only (split triml)]) (def n (Integer/parseInt (read-line))) (loop [i 0] (when (< i n) (def a (read-line)) (def new (split a #"\s+")) (println ( + (Integer/parseInt (get new 0)) (Integer/parseInt (get new 1)) )) (recur (inc i)) ))
Solution
Perl
$n = <>; for($i=0; $i<$n; $i++) { $lis = <>; @ll = split(' ', $lis); print $ll[0] + $ll[1]; print "\n"; }
Solution
Haskell
import Control.Monad main :: IO () main = do n <- readLn :: IO Int str <- replicateM n getLine let ans = map (sum. map read. words) str mapM_ print ans
Solution
Erlang
-module(solution). -export([main/0]). main() -> {ok, [N]} = io:fread("", "~d"), test(N). test(0) -> ok; test(N) -> {ok, [A, B]} = io:fread("", "~d ~d"), io:fwrite("~w~n", [(A + B)]), test(N-1).
Solution
Go lang
package main import "fmt" func main() { var n int var a int var b int fmt.Scan(&n) for i := 0; i < n; i++ { var sum int fmt.Scan(&a,&b) sum = a+b fmt.Println(uint(sum)) } }
Solution
Groovy
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) def a,b, res,line def n = Integer.parseInt(br.readLine()) for (int i = 0; i < n; i++) { line = br.readLine() def (_a,_b) = line.split(' ') a = Integer.parseInt(_a) b = Integer.parseInt(_b) res = a+b println res }
Solution
Scala
object Solution extends App { val n = readInt //recommended (1 to n).map(i => readLine.split(" ").map(_.toInt).sum).foreach(println) /* another way for (i <- 1 to n) { val Array(a, b) = readLine.split(" ").map(_.toInt) println(a + b) } */ }
Solution
R
f <- file("stdin") open(f) count = as.numeric(readLines(f, n = 1)) for(i in 1:count) { lines <- readLines(f, n = 1) num <- strsplit(lines, " ")[[1]] sum <- as.numeric(num[1]) + as.numeric(num[2]) write(sum, stdout()) }
Solution
Swift
import Foundation let t = Int(readLine()!)! for i in 1...t { let nums = readLine()!.characters.split(" ").map{Int(String($0))!} let a = nums[0] let b = nums[1] print(a + b) }
Comments
0 comments
Please sign in to leave a comment.