CodeChef Logo CodeChef Logo
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Upgrade to Pro
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Programming and DSA

Explore courses

Catalogue

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

Explore courses
Practice Compete Compiler
Home » Wiki » Sample Solutions

Sample Solutions

 

 

Table of Contents 
  1. Submitting a Solution :
  2. C++
  3. C
  4. Pascal
  5. Java
  6. Jar
  7. Nice
  8. Python
  9. Lisp
  10. Scheme
  11. OCaml
  12. Haskell
  13. Clips
  14. Prolog
  15. C#
  16. Brainf*ck
  17. Perl
  18. Ruby
  19. Pike
  20. PHP
  21. Intercal
  22. NASM
  23. Ada95
  24. Bash
  25. Smalltalk
  26. Fortran
  27. Icon
  28. Erlang

 

Submitting a Solution :


To submit a solution choose problem from list of problems and press button 'Submit' near the top right corner of the problem page. You can submit multiple solutions to each problem. Score for the problem is equal to the score of the best submitted solution.

To see the Statistic for problem choose problem from list of problems and press button 'All submissions' at the top of the problem description. To view the status, hover over the check box, cross or warning icon in the result column.

Solutions in different languages need to be structured in particular ways. For example, the public class in Java needs to be named as Main. Here are a few sample solutions in different languages for a very elementary problem statement.

Solutions to TEST are given below in 26 programming languages. Feel free to respond with questions/comments/suggestions.

 

C++


[code]

#include <iostream>

int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}
[/code]

C


[code]


#include <stdio.h>

int main(void) {
int x;
for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x));
return 0;
}
[/code]

Pascal


[code]


program test;
var x: integer;
begin
repeat
readln(x);
if x<>42 then writeln(x);
until x=42
end.
[/code]

Java


[code]


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);
}
}
[/code]

Jar


  • Make a file main-class.txt. From the Bash prompt: echo -e 'Main-Class: Main\r' > main-class.txt
  • Copy Main.class and any other necessary classes and resources into a subdirectory of your work directory called jarstuff: mkdir jarstuff; cp Main.class resources.zip jarstuff
  • Create the executable jar: jar cfm MySolution.jar main-class.txt -C jarstuff . Make sure to include that final dot!

Nice


[code]


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);
}
[/code]

Python


[code]


k=raw_input()
while int(k)!=42:
print k
k=raw_input()
[/code]

Lisp


[code]


(loop for l = (read-line)
for n = (parse-integer l)
until (= n 42) do (format t "~D~%" n))
[/code]

Scheme


[code]


(do ((i (read) (read)))
((eq? i 42) '())
(begin
(display i)
(newline)))
[/code]

OCaml


[code]


while true do
let n = read_int () in
if n=42 then exit 0 else print_int n; print_newline ()
done
[/code]

Haskell


[code]


main = interact f
where f = unlines . takeWhile (/="42") . words
[/code]

 

Clips


[code]


(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))
)
[/code]

Prolog


[code]


program :- get_char(X),get_char(Y),check(X,Y).
check('4','2'):-!.
check(X,Y):-write(X),get_char(Z),check(Y,Z).
[/code]

 

C#


[code]


using System;
public class Test
{
public static void Main()
{
int n;
while ((n = int.Parse(Console.ReadLine()))!=42)
Console.WriteLine(n);
}
}
[/code]

 

Brainf*ck


[code]


+[>>----------
[++++++++++<,----------]
>--------------------------------------------------
>----------------------------------------------------
>

[
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]>>
]<

[++++++++++++++++++++++++++++++++++++++++++++++++++ ++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]>
]<

[>+++++++++++++++++++++++++++++++++++++++++++++++ ++ +++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]
]<

]

[/code]

Perl


[code]

print while($_=<>)!=42

[/code]

Ruby


[code]


print while gets != "42\n"

[/code]

Pike


[code]


int main() {
while (sscanf(Stdio.stdin->gets(), "%d", int n), n!=42 && write(n+"\n"));
return 0;
}
[/code]

PHP


[code]


<?php
while (true) {
$input = fgets(STDIN, 3);
if ($input == 42) {
break;
} else {
echo $input;
}
}
?>
[/code]

 

Intercal


[code]


PLEASE DO ,1 <- #1
PLEASE DO .4 <- #0
PLEASE DO .5 <- #0
PLEASE DO .99 <- #0
DO COME FROM (30)
DO COME FROM (31)
DO WRITE IN ,1
DO .1 <- ,1SUB#1
DO .2 <- .4
DO (1000) NEXT
DO .4 <- .3~#255
DO (10) NEXT
(42) DO .1 <- .1
(20) DO .42 <- "&'&.4~#26'$#1"
PLEASE RESUME "?.42$#1"~#3
(10) DO (20) NEXT
DO FORGET #1
PLEASE COME FROM (42)
PLEASE STASH .1+.2+.3
DO .1 <- .4
DO .2 <- #50
DO (1010) NEXT
DO (100) NEXT
PLEASE STASH .1+.2+.3
DO .1 <- .99
DO .2 <- #52
DO (1010) NEXT
DO (101) NEXT
PLEASE GIVE UP
(201) DO .3 <- '.3~.3'~#1
PLEASE RESUME "?.3$#2"~#3
(101) DO (201) NEXT
DO FORGET #1
(32) PLEASE RETRIEVE .1+.2+.3
(200) DO .3 <- '.3~.3'~#1
PLEASE RESUME "?.3$#2"~#3
(100) DO (200) NEXT
DO FORGET #1
DO COME FROM (32)
PLEASE RETRIEVE .1+.2+.3
DO (102) NEXT
(31) DO .99 <- .4
(202) DO .98 <- '.99~.99'~#1
PLEASE RESUME "?.98$#2"~#3
(102) DO (202) NEXT
DO FORGET #1
DO .3 <- !99~#15'$!99~#240'
DO .3 <- !3~#15'$!3~#240'
DO .2 <- !3~#15'$!3~#240'
DO .1 <- .5
DO (1010) NEXT
DO .5 <- .2
DO ,1SUB#1 <- .3
PLEASE READ OUT ,1
(30) DO .99 <- .4
[/code]

NASM


[code]


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
[/code]

 

Ada95


[code]


with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure test is
x : integer;
begin
loop
get(x);
if x/=42
then
put(x);
new_line(1);
else
exit;
end if;
end loop;
end test;
[/code]

 

Bash


[code]


while read a; do
if [ $a -eq 42 ]; then
exit
else
echo $a
fi
done
[/code]

Smalltalk


[code]

|c number|
[
number:=0.
[ (c := stdin next) asciiValue ~= 10 ]
whileTrue:
[number := (number * 10) + (c asciiValue) - 48.].
number ~= 42
]
whileTrue:
[Transcript show: number printString; cr.]
!
[/code]

 

Fortran


[code]


program TEST
integer ans
do
read (*,*) ans
if (ans.eq.42) stop
write (*,*) ans
enddo
stop
end
[/code]

 

Icon


[code]


procedure main ()
while (l := read()) ~= 42 do
write(l);
end

[/code]

 

Erlang


[code]

-module(tested).
-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.

[/code]

yes we can use arrays but

1★joy_the_coder @ 20 Jun 2015 05:40 PM
yes we can use arrays but since the code is being judged by a machine , so it hardly matters that whether we should use arrays or not. as machine knows what are the input terms and what are their corresponding outputs
Workden, MNR PRIDE, 14, HAL Old Airport Rd, Domlur I Stage, 1st Stage, DOMLUR, Bengaluru, Karnataka 560071 [email protected] +91 95911 47880
Find us online

ROADMAPS

Learn Python
Learn Java
Learn C
Learn C++
Data structures and Algorithms
Competitive Programming
More Roadmaps

CAREER PATHS

React JS Developer
Full stack Developer
SQL for Data Analysis
Frontend Developer
Java Backend Developer
Data Analysis using Python
Python Backend Developer
C++ Developer
Machine Learning using Python

COMPILERS

HTML online compiler
C++ online compiler
C online compiler
Java online compiler
Python online compiler
SQL online compiler
JavaScript online compiler
React online compiler
More compilers

COMPANY

About us
For colleges
Coding Contests
Blogs
Contact us
Privacy Policy
Frequently Asked Questions

© 2025 CodeChef Inc. All rights reserved.

We use cookies to improve your experience and for analytical purposes. Read our Privacy Policy and Terms to know more. You consent to our cookies if you continue to use our website.