Few samples of codes are presented below. You can click run to execute the code.
- go to:
- Ada
- Assembler
- Assembler
- AWK (gawk)
- AWK (mawk)
- Bash
- bc
- Brainf**k
- C
- C#
- C++ 4.3.2
- C++ 4.7.2
- C++11
- C99 strict
- CLIPS
- Clojure
- COBOL 85
- Common Lisp (clisp)
- D (dmd)
- Erlang
- F#
- Factor
- Falcon
- Fortran
- Go
- Groovy
- Haskell
- Icon
- Intercal
- Java
- Java7
- JavaScript (rhino)
- JavaScript (spidermonkey)
- Lua
- Nemerle
- Nice
- Nimrod
- Node.js
- Objective-C
- Ocaml
- Octave
- Oz
- PARI/GP
- Pascal (fpc)
- Pascal (gpc)
- Perl
- Perl 6
- PHP
- Pike
- Prolog (swi)
- Python
- Python 3
- R
- Ruby
- Scala
- Scheme (guile)
- Smalltalk
- SQL
- Tcl
- Text
- VB.NET
- Whitespace
Ada (gnat-4.6)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | with Ada.Integer_Text_Io, Ada.Text_Io; use Ada.Integer_Text_Io, Ada.Text_Io; procedure Test is subtype Small is Integer range 0..99; Input : Small; begin loop Get(Input); if Input = 42 then exit; else Put (Input); New_Line; end if; end loop; end; |
Assembler (gcc-4.7.2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | .data x: .long 0 s: .string "%d\n\0" .text .global main main: # int main() # { loop: # for (;;) { pushl $x # scanf("%d", &x); pushl $s call scanf addl $8, %esp movl x, %eax # if (x == 42) break; subl $42, %eax jz break pushl x # printf("%d\n", x); pushl $s call printf addl $8, %esp jmp loop # } break: xor %eax, %eax # return 0; ret # } |
Assembler (nasm-2.10.01)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | global _start section .data buffer dw 0h section .text _start: mov ecx, buffer mov edx, 02h call read mov cx, word [buffer] cmp cx, 3234h je exit cmp ch, 0ah je one_dig jmp two_dig one_dig: mov ecx, buffer mov edx, 02h call write jmp _start two_dig: mov ecx, buffer mov edx, 02h call write mov edx, 01h mov ecx, buffer call read ; read the 0ah mov ecx, buffer call write ; write the 0ah jmp _start exit: mov eax, 01h ; exit() xor ebx, ebx ; errno int 80h read: mov eax, 03h ; read() mov ebx, 00h ; stdin int 80h ret write: mov eax, 04h ; write() mov ebx, 01h ; stdout int 80h ret |
AWK (gawk) (gawk-3.1.6)
run
download
1 2 3 4 5 6 7 | { num = $1; if(num == 42) exit; else printf("%d\n", num); } |
AWK (mawk) (mawk-1.3.3)
run
download
1 2 3 4 5 6 7 | { num = $1; if(num == 42) exit; else printf("%d\n", num); } |
Bash (bash 4.0.35)
run
download
1 2 3 4 5 6 7 8 9 10 | #!/bin/bash while true do read line if [ $line -eq 42 ] then exit 0 fi echo $line done |
bc (bc-1.06.95)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* read the numbers; notice: ech line of the input must be followed by an EOF character */ x = read(); /* multiplication table */ for (i=1; i<=x; ++i) { for (j=1; j<=x; ++j) print i*j, "\t" print "\n" } /* compute the pi number accurately to 10 decimal places */ scale=x print "\npi = ", 4*a(1), "\n" /* factorial */ define f(n) { if (n <= 1) return 1; return n * f(n-1); } print "\n"; print "1! = ", f(1), "\n"; print "5! = ", f(5), "\n"; print x, "! = ", f(x), "\n"; |
Brainf**k (bff-1.0.3.1)
run
download
1 2 3 4 5 6 | >+[>>,----------[>,----------]+++++[<-------->-]<[>>-<]>+[ -<+++++++[<------>-]<[>>-<]>+[ -<<[>>-<]>+[<<->>->]> ]<+++++++[<++++++>-]>> ]<++++++++[<+++++>-]< [<]<[>>[++++++++++.>]++++++++++.[[-]<]]<] |
C (gcc-4.7.2)
run
download
1 2 3 4 5 6 7 | #include <stdio.h> int main(void) { int x; for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x)); return 0; } |
C# (mono-2.8)
run
download
1 2 3 4 5 6 7 8 9 10 | using System; public class Test { public static void Main() { int n; while ((n = int.Parse(Console.ReadLine()))!=42) Console.WriteLine(n); } } |
C++ 4.3.2 (gcc-4.3.2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { int intNum = 0; cin >> intNum; while (intNum != 42) { cout << intNum << "\n"; cin >> intNum; } return 0; } |
C++ 4.7.2 (gcc-4.7.2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { int intNum = 0; cin >> intNum; while (intNum != 42) { cout << intNum << "\n"; cin >> intNum; } return 0; } |
C++11 (gcc-4.7.2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; int main() { int intNum = 0; cin >> intNum; while (intNum != 42) { cout << intNum << "\n"; cin >> intNum; } return 0; } |
C99 strict (gcc-4.7.2)
run
download
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> int main(void){ int liczba; for ( ; (scanf("%d",&liczba) > 0) && (liczba != 42) ; printf("%d\n", liczba) ); return 0; } |
CLIPS (clips 6.24)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | (defrule readin ?f<-(initial-fact) => (retract ?f) (assert (number (read))) ) (defrule writeout ?f<-(number ?n)(test (<> ?n 42)) => (retract ?f) (printout t ?n crlf) (assert (initial-fact)) ) (reset) (run) (exit) |
Clojure (clojure 1.5.0-RC2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | (defn bottles [n & [capitalize]] (str (if (> n 0) n (if capitalize "No more" "no more")) " bottle" (if (= 1 n) "" "s") " of beer" )) (defn bot-wall [n & cap] (str (bottles n cap) " on the wall")) (defn sing ; Default is 99 times. ([] (sing 99)) ([stock] (doseq [i (range stock -1 -1)] (printf "%s, %s.\n%s.\n\n" (bot-wall i true) (bottles i) (apply str (if (> i 0) ["Take one down and pass it around, " (bot-wall (dec i))] ["Go to the store and buy some more, " (bot-wall stock)] )))))) (sing) |
COBOL 85 (tinycobol-0.65.9)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 77 n PIC Z9 . PROCEDURE DIVISION. ACCEPT n PERFORM UNTIL n = 42 DISPLAY n ACCEPT n END-PERFORM. STOP RUN. |
Common Lisp (clisp) (clisp 2.47)
run
download
1 2 3 4 | (loop for line = (read-line *standard-input* nil nil) while (not (equal line "42")) do (format t "~A~%" line)) |
D (dmd) (dmd-2.042)
run
download
1 2 3 4 5 6 7 | import std.c.stdio; int main() { int x; while (scanf("%d", &x) && x!=42) printf ("%d\n", x); return 0; } |
Erlang (erl-5.7.3)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | -module(prog). -export([main/0]). main() -> loop(). loop() -> case io:fread( "","~d" ) of eof -> true; {ok, X} -> [Y] = X, if Y == 42 -> true; true -> io:fwrite( "~B\n",X ), loop() end end. |
F# (fsharp-2.0.0)
run
download
1 2 3 4 5 6 7 8 | open System let mutable num = System.Console.ReadLine() while not(System.String.Equals(num, "42", System.StringComparison.CurrentCultureIgnoreCase)) do System.Console.Write(num) num <- System.Console.ReadLine() System.Console.WriteLine() |
Factor (factor-0.93)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ! Copyright (C) 2006 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. USING: kernel math sequences strings io combinators ascii ; IN: rot13 : rotate ( ch base -- ch ) [ - 13 + 26 mod ] [ + ] bi ; : rot-letter ( ch -- ch ) { { [ dup letter? ] [ CHAR: a rotate ] } { [ dup LETTER? ] [ CHAR: A rotate ] } [ ] } cond ; : rot13 ( string -- string ) [ rot-letter ] map ; : rot13-demo ( -- ) "Please enter a string:" print flush readln [ "Your string: " write dup print "Rot13: " write rot13 print ] when* ; MAIN: rot13-demo rot13-demo |
Falcon (falcon-0.9.6.6)
run
download
1 2 3 4 5 6 | x = int(input()) while x != 42 printl(x) x = int(input()) end |
Fortran (gfortran-4.7.2)
run
download
1 2 3 4 5 6 7 8 9 | program TEST integer ans do read (*,*) ans if (ans.eq.42) stop write (*,*) ans enddo stop end |
Go (1.0.3)
run
download
1 2 3 4 5 6 7 8 9 10 11 | package main import "fmt" func main(){ var n int fmt.Scanf("%d",&n) for n!=42 { fmt.Printf("%d\n",n) fmt.Scanf("%d",&n) } } |
Groovy (groovy-2.1.0-rc-1)
run
download
1 2 3 4 5 6 7 8 9 10 | class Greet { def name Greet(who) { name = who[0].toUpperCase() + who[1..-1] } def salute() { println "Hello $name!" } } g = new Greet('world') // create object g.salute() |
Haskell (ghc-7.4.1)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 | main = do x <- readNum if x == 42 then putStr("") else do putStr (show (x) ++ "\n") main where readNum :: IO Integer readNum = do line <- getLine readIO line |
Icon (iconc 9.4.3)
run
download
1 2 3 4 | procedure main () while (l := read()) ~= 42 do write(l); end |
Intercal (c-intercal 28.0-r1)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | DO ,915 <- #1 DO ,2216 <- #2 DO .2216 <- #1 DO .129 <- #0 DO .1215 <- #0 (19) PLEASE DON'T FORGET #1 DO REINSTATE FORGETTING PLEASE DO (18514) NEXT DO .2 <- #10 DO (1) NEXT PLEASE NOTE THERE WAS A NEWLINE DO .1 <- .2216 DO .2 <- #3 DO (11) NEXT DO .1 <- ,2216 SUB #1 DO .2 <- #52 DO (11) NEXT DO .1 <- ,2216 SUB #2 DO .2 <- #50 DO (11) NEXT PLEASE GIVE UP (11) DO (31513) NEXT (12) DO FORGET #1 DO .8 <- #1 DO COME FROM (998) DO .1 <- .8 DO .2 <- .2216 DO (3) NEXT DO .1 <- #10 PLEASE DO (23189) NEXT DO .2216 <- #1 PLEASE DO (19) NEXT (3) DO (31513) NEXT PLEASE FORGET #1 DO .1 <- ,2216 SUB .8 PLEASE DO (23189) NEXT DO .1 <- .8 DO .2 <- #1 PLEASE DO (1000) NEXT (998) DO .8 <- .3 (1) DO (31513) NEXT PLEASE NOTE THERE WAS NO NEWLINE PLEASE FORGET #1 DO ,2216 SUB .2216 <- .1 DO .1 <- .2216 DO .2 <- #1 PLEASE DO (1000) NEXT DO .2216 <- .3 DO (19) NEXT (31513) PLEASE NOTE RESUMES #2 IF EQUAL ELSE #1 DO RESUME '?#32768$"#65535~'"?.1$.2"~"#0$#65535"'"'~'#32768$#1' (18514) PLEASE STASH .2+.3 DO WRITE IN ,915 DO .1 <- ,915 SUB #1 DO .2 <- .128 PLEASE DO (1000) NEXT DO .1 <- .3~#255 DO .128 <- .1 PLEASE RETRIEVE .2+.3 DO RESUME #1 (23189) PLEASE STASH .1+.2+.3 DO .1 <- !1~#15'$!1~#240' DO .1 <- !1~#15'$!1~#240' DO .1 <- !1~#15'$!1~#240' DO .2 <- .1 DO .1 <- '#256$.129'~'#256$#255' DO .129 <- .2 PLEASE DO (1010) NEXT DO ,915 SUB #1 <- .3~#255 DO READ OUT ,915 PLEASE RETRIEVE .1+.2+.3 DO RESUME #1 PLEASE REMEMBER TO GIVE UP |
Java (sun-jdk-1.7.0_10)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 | /* package whatever; // don't place package name! */ /* The class name doesn't have to be Main, as long as the class is not public. */ class Main { public static void main (String[] args) throws java.lang.Exception { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String s; while (!(s=r.readLine()).startsWith("42")) System.out.println(s); } } |
Java7 (sun-jdk-1.7.0_10)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 | /* package whatever; // don't place package name! */ /* The class name doesn't have to be Main, as long as the class is not public. */ class Main { public static void main (String[] args) throws java.lang.Exception { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String s; while (!(s=r.readLine()).startsWith("42")) System.out.println(s); } } |
JavaScript (rhino) (rhino-1.7R4)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | importPackage(java.io); importPackage(java.lang); var reader = new BufferedReader( new InputStreamReader(System['in']) ); while (true){ var line = reader.readLine(); if (line==null || line=="42") { break; } else { System.out.println(line); } } |
JavaScript (spidermonkey) (spidermonkey-1.7)
run
download
1 2 3 | while((num = readline()) != 42) { print(num); } |
Lua (luac 5.1.4)
run
download
1 2 3 4 5 6 7 | local read, write = io.read, io.write local num, nl = '*n', '\n' while true do local a = read(num) if a == 42 then return end write(a, nl) end |
Nemerle (ncc 0.9.3)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; public class Test { public static Main() : void { def solve() : void { def i = int.Parse(Console.ReadLine()); unless (i == 42) { Console.WriteLine(i); solve(); } } solve(); } } |
Nice (nicec 0.9.6)
run
download
1 2 3 4 5 6 | void main (String[] args) { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String s; while (!(s=notNull(r.readLine())).startsWith("42")) System.out.println(s); } |
Nimrod (nimrod-0.8.8)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | proc GetBottleNumber(n: int): string = var bs: string if n == 0: bs = "No more bottles" elif n == 1: bs = "1 bottle" else: bs = $n & " bottles" return bs & " of beer" for bn in countdown(99, 1): var cur = GetBottleNumber(bn) echo(cur, " on the wall, ", cur, ".") echo("Take one down and pass it around, ", GetBottleNumber(bn-1), " on the wall.\n") echo "No more bottles of beer on the wall, no more bottles of beer." echo "Go to the store and buy some more, 99 bottles of beer on the wall." |
Node.js (0.8.11)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | process.stdin.resume(); process.stdin.setEncoding('utf8'); var remainder = '' process.stdin.on('data', function (chunk) { var lines = chunk.toString().split('\n'); lines.unshift(remainder + lines.shift()); remainder = lines.pop(); lines.forEach(function(line) { if (line === '42') { process.exit(); } process.stdout.write(line+'\n'); }); }); |
Objective-C (gcc-4.5.1)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #import <objc/objc.h> #import <objc/Object.h> @interface TestObj : Object { int num; } - (void)foo; @end @implementation TestObj - (void)foo { printf("foo\n"); } int main() { id obj = [[TestObj alloc] init]; [obj foo]; return 0; } @end |
Ocaml (ocamlopt 3.10.2)
run
download
1 2 3 4 5 6 | while true do let n = read_int () in if n = 42 then exit 0; print_int n; print_newline () done;; |
Octave (3.6.2)
run
download
1 2 3 4 5 6 7 8 9 10 | x = [1 2 3 4 5 6 7 8 9 10]; y1 = [.16 .08 .04 .02 .013 .007 .004 .002 .001 .0008 ]; y2 = [.16 .07 .03 .01 .008 .003 .0008 .0003 .00007 .00002 ]; semilogy(x,y1,'-bo;y1;',x,y2,'-kx;y2;'); title('Plot title'); xlabel('X Axis'); ylabel('Y Axis'); print out.png |
Oz (mozart-1.4.0)
run
download
1 2 3 4 5 6 7 8 9 10 11 | functor import Application System define {System.showInfo 'Hello World!'} {Application.exit 0} end |
Pascal (fpc) (fpc 2.6.2)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 | program test; var x:byte; begin readln(x); while x<>42 do begin writeln(x); readln(x) end end. |
Pascal (gpc) (gpc 20070904)
run
download
1 2 3 4 5 6 7 8 | program test; var x: integer; begin repeat readln(x); if x<>42 then writeln(x); until x=42 end. |
PHP (php 5.4.4)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $hi = fopen('php://stdin', "r"); $ho = fopen('php://stdout', "w"); while (true) { fscanf($hi, "%d", $n); if ($n == 42) break; fwrite($ho, sprintf("%d\n", $n)); } fclose($ho); fclose($hi); ?> |
Pike (pike 7.6.86)
run
download
1 2 3 4 5 6 7 8 | int main() { string s=Stdio.stdin->gets(); while (s!="42") { write(s+"\n"); s=Stdio.stdin->gets(); } return 0; } |
Prolog (swi) (swipl 5.6.64)
run
download
1 2 3 4 | program :- get_char(X),get_char(Y),check(X,Y). check('4','2'):-!. check(X,Y):-write(X),get_char(Z),check(Y,Z). :- program. |
Python (python 2.7.3)
run
download
1 2 3 4 | n = int(raw_input()) while n != 42: print n n = int(raw_input()) |
Python 3 (python-3.2.3)
run
download
1 2 3 4 | n = int(input()) while n != 42: print(n) n = int(input()) |
R (R-2.11.1)
run
download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | song<-function(n=99) { a<-" bottle" b<-" of beer on the wall, " c<-" of beer. " d<-"Take one down and pass it around: " # ## set "beer counter" vector counter<-c(as.character(n:1),"No more") # ## set plural s<-ifelse(counter=="1","","s") # ## build up the verses vector firsthalf<-paste(counter,a,s,b,counter,a,s,c,sep="") sechalf1.99<-paste(d,counter[-1],a,s[-1],b,sep="") sechalf100<-paste("Go to the store and buy some more: ", counter[1],a,"s",b,sep="") ## ## song is the vector of n+1 complete verses song<-paste(firsthalf,c(sechalf1.99,sechalf100),sep="") } print(song()) |
Scala (scala-2.10.0)
run
download
1 2 3 4 5 6 7 | object Main extends App { var line = readLine(); while(false == line.equals("42")) { System.out.println(line); line = readLine(); }; } |
Scheme (guile) (guile 1.8.5)
run
download
1 2 3 4 5 6 7 8 9 10 | (define (do_it n) (define (print_it n) (display n) (newline)) (cond ((not(= n 42)) (print_it n) (do_it (read))) )) (do_it (read)) |
Smalltalk (gst 3.1)
run
download
1 2 3 4 5 6 7 8 9 10 11 | |c number| [ number:=0. [ (c := stdin next) asciiValue ~= 10 ] whileTrue: [number := (number * 10) + (c asciiValue) - 48.]. number ~= 42 ] whileTrue: [Transcript show: number printString; cr.] ! |
SQL (sqlite3-3.7.3)
run
download
1 2 3 | create table tbl(str varchar(20)); insert into tbl values('Hello world!'); select * from tbl; |
Tcl (tclsh 8.5.7)
run
download
1 2 | set liczba [gets stdin] while { $liczba != 42 } { puts $liczba; set liczba [gets stdin] } |

