Sample set of programs in various language versions

Source: Internet
Author: User
Tags lua

sample set of programs in various language versions

The following sample program can be used to solve this simple problem: read 2 integers A and B, and then output their and.

awk (. awk)

{print $1 + $2}

Bash (. Sh)

while read i; do  echo $((${i/ /+}))done

Clang (. c)

#include <stdio.h>int main(){  int a, b;  while(scanf("%d %d",&a, &b) != EOF)    printf("%d\n", a + b);  return 0;}

clang++ (. cpp)

#include <iostream>using namespace std;int main(){  int a, b;  while (cin >> a >> b)    cout << a+b << endl;  return 0;}

Clisp (. Cl)

(loop for n = (read t nil nil)      while n      do (format t "~d~C" (+ n (read)) #\linefeed))

FPC (. Pas)

var  a, b: integer;begin  while not eof(input) do begin    readln(a, b);    writeln(a + b);  end;end.

GCC (. c)

#include <stdio.h>int main(){  int a, b;  while(scanf("%d %d",&a, &b) != EOF)    printf("%d\n", a + b);  return 0;}

g++ (. cpp)

#include <iostream>using namespace std;int main(){  int a, b;  while (cin >> a >> b)    cout << a+b << endl;  return 0;}

Gccgo, go (. Go)

package mainimport "fmt"func main() {  var a, b int  for {    n, _ := fmt.Scanf("%d %d", &a, &b)    if (n != 2) { break }    fmt.Println(a + b)  }}

GCJ (. Gcj.java)

import java.util.Scanner;public class Main {    public static void main(String[] args) {      Scanner in = new Scanner(System.in);    try {      while (in.hasNextInt()) {        int a = in.nextInt();        int b = in.nextInt();        System.out.println(a + b);      }    } catch (NullPointerException ex) {      // gcj Scanner has a bug that throws NPE      ;    }}}

GHC (. HS)

main = interact $ unlines . map (show . sum . map read . words) . lines

Javac (. java)

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        while (in.hasNextInt()) {            int a = in.nextInt();            int b = in.nextInt();            System.out.println(a + b);        }    }}

Lua (. Lua)

for a, b in io.read(‘*a‘):gmatch(‘([%d-]+) ([%d-]+)‘) do  print(tonumber(a) + tonumber(b))end

MCS (. cs)

public class Program {  public static void Main() {    string line;    while ((line = System.Console.ReadLine ()) != null) {      string[] tokens = line.Split();      System.Console.WriteLine(int.Parse(tokens[0]) + int.Parse(tokens[1]));    }  }}

Node (. js)

var fs = require(‘fs‘);var buf = ‘‘;process.stdin.on(‘readable‘, function() {  var chunk = process.stdin.read();  if (chunk) buf += chunk.toString();});process.stdin.on(‘end‘, function() {  buf.split(‘\n‘).forEach(function(line) {    var tokens = line.split(‘ ‘).map(function(x) { return parseInt(x); });    if (tokens.length != 2) return;    console.log(tokens.reduce(function(a, b) { return a + b; }));  });});

OCAMLC (. ml)

try  while true do    Scanf.scanf " %d %d" (fun a b -> Printf.printf "%d\n" (a + b))  done;  Nonewith  End_of_file -> None;;

Perl (. PL)

#!/usr/bin/perl -pla$_ = $F[0] + $F[1]

PHP (. php)

<?phpwhile (fscanf(STDIN, "%d%d", $a, $b) == 2) {  print ($a + $b) . "\n";}

Python2, 3 (. py) (note that Python3 should use the input () read input)

import sysfor line in sys.stdin:    print(sum(map(int, line.split())))

Racket (. Rkt)

#lang racket(define (f)  (let ([n (read)])    (if (eof-object? n)        (void)        (begin (displayln (+ (read) n))               (f)))))(f)

Ruby (. RB)

STDIN.each_line do |line|  puts line.split.map(&:to_i).inject(:+)end

Valac (. Vala)

public static int main(string[] args) {  int a = 0, b = 0;  while (stdin.scanf("%d%d", &a, &b) == 2) {    stdout.printf("%d\n", a + b);  }  return 0;}

VBNC (. vb)

Module Program  Sub Main()    Do      Dim line As String = Console.ReadLine()      If line Is Nothing Then Exit Do      Dim a() As String = line.Split(" "c)      Console.WriteLine(CInt(a(0)) + CInt(a(1)))    Loop  End SubEnd Module

Sample set of programs in various language versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.