# print 1 to 100 without using recursion and conditions [closed]

can we print 1 to 100 without using any if conditions and loops in c&c++?

Conditon: main point is you must not use recursion...and doesnt hardcode code in it for e.g

print(1 2 3..etc);

• print "1 to 100" or print 1 2 3 4 5 6 (etc)? – beary605 Dec 27 '12 at 7:19
• @beary605 print 1 2 3..100 – BlueBerry - Vignesh4303 Dec 27 '12 at 7:21
• Does goto count? – Ry- Dec 31 '12 at 0:50
• Do preprocessor conditions count? – ApproachingDarknessFish Jan 16 '14 at 21:50

## C (90) (79) (59) (47) (42) (40)

static int

x=1;a(){char b[8];printf("%d\n",x++);b[24]-=5*(1-x/101);}main(){a();return 0;}


The function a which prints the numbers does not call itself! I exploited a buffer overflow and changed the return address to make the program counter go over function a again as long as I need.

I don't know if this is considered to be a recursion, but I thought it would worth trying. This code works on my 64-bit machines with gcc 4.6, for other platforms the last statement of function a, could be a little different.

Exp1: I allocated a dummy buffer on stack b, and then addressed a passed-by-end location, which is the location of return address. I anticipated the distance between start of buffer and return address location from disassembly of function a.

Exp2: Expression 5*(1-x/101), is 5 for all x<=100 and 0 for x=101. By looking at disassembly of main (in my case), if you decrease the return address by 5, you will set the PC to calling point of a again. In the updated codes, the return value of printf is used for checking loop condition.

Update: After applying ugoren's suggestions and some other changes:

x;a(){int b[2];b[3*(printf("%d\n",++x)&2)]-=5;}main(){a();}


Update2: After Removing function a:

x;main(){int b[2];b[6^printf("%d ",++x)&4]-=7;}


Update3:

x;main(b){(&b)[1|printf("%d ",++x)&2]-=7;}


Update4: Thanks to mbz :)

x;main(b){(&b)[3|printf("%d ",++x)]-=7;}

• Perhaps the most interesting answer here! Could you explain a bit about how it's supposed to work? How did you come with b[24] for the location of the PC and how can I determine what I should use instead (32-bit cygwin, gcc 4.7.3)? What is the rationale behind the magical expression 5*(1-x/101)? Also, is the static part of x's declaration needed? I'm not fully clear on how this works but since we aren't explicitly returning from a() wouldn't even an auto variable x remain in memory? And in this case it's even a global already. Thanks again for enlivening this dull repetitive thread. – sundar - Reinstate Monica Aug 17 '13 at 9:50
• @sundar Thank you for pointing out static. You're right, It was necessary for my previous code and I forgot to remove it. I added some small explanations, please check them out in answer :) – saeedn Aug 19 '13 at 3:36
• 1. int b[2] would be shorter. 2. return 0; isn't needed. 3. Changing the index on b instead of the subtracted value would be shorter. (of course, every such change could change machine code and break the solution, but I think it should be OK). – ugoren Aug 19 '13 at 5:01
• Another idea - maybe exploit the fact that printf returns a higher value at 100? – ugoren Aug 19 '13 at 5:02
• @saeedn Thanks for the explanations, it's very clear now. I felt stupid after reading Exp2 though, I'd been trying to decode it as some overcomplicated int-to-float bit pattern thing like the famous "inverse square root" formula, missed this much simpler explanation. :) – sundar - Reinstate Monica Aug 19 '13 at 18:29

# 85

C (gcc)

#define c printf("%d ",i++);
#define b c c c c c
#define a b b b b b
main(i){a a a a}


Assuming no command line arguments were passed.

• Using the preprocessor is almost like hard coding it. – Martin Thoma Jan 1 '13 at 16:52
• agreed. This is hardwired. – Cong Hui Nov 9 '13 at 8:01

## C++ (159 136)

With templates.

#include<cstdio>
#define Z(A,B,C,D)template<A>struct P B{P(){C;printf("%d ",D);}};
Z(int N,,P<N-1>(),N)Z(,<1>,0,1)int main(){P<100>();}

• s/class/struct/;s/public://;s/static //;s/::/()./g saves 11 characters. – ceased to turn counterclockwis Dec 30 '12 at 19:49

## C 71 70

Assuming the ? operator is allowed.

#define f(a)a a a a a
int main(i){f(f(f(printf(i<102?"%d ":0,i++);)))}


Edit: ""->0

If ? is too similar to an if statement, then use this instead (78)

#define f(a)a a a a
#define g(a)f(a)a
int main(i){f(g(g(printf("%d ",i++);)))}

• ? is a conditional is it not? – Claudiu Jan 10 '13 at 14:40
• vignesh4303 disallowed "if conditions", not all conditionals, which is why I included both answers. – cardboard_box Jan 11 '13 at 2:24

# 267

this is the best I can think of, assuming using the preprocessor is fine.

#include <stdio.h>
#define a(i)i,i+1,i+2,i+3
#define b(i)a(i),a(i+4),a(i+8),a(i+12)
#define c(i)b(i),b(i+16)
#define e c(1),c(33)
#define f %d %d %d %d
#define g f f f f f f f f
#define r(m) #m
#define s(m) r(m)
int main(){printf(s(g g g f),e,c(65),a(97));return 0;}


# C++ (115)

#include <cstdio>
template<int i>void p(){printf("%d ",i);p<i+1>();}
template<>void p<101>(){}
int main(){p<1>();}

• Isn't this recursion? – Bryan Chen Sep 18 '13 at 5:47

# MATLAB/Octave, (5 chars)

1:100

• Doesn't the question want a code in C? – saeedn Aug 15 '13 at 21:40

## PowerShell (6 characters)

1..100

• Equally doable, though no shorter, with 1..1e2. – Iszi Nov 16 '13 at 16:21

# Python 3 (25)

print(list(range(1,101)))

• it is a inbuilt function that uses recursion! – Pranit Bauva Jan 5 '13 at 14:21
• I guess it depends on the interpreter if range uses recursion. I don't use recursion in my solution. I don't care if the inbuilt functions use recursion. If you want to take a look at every possible interpreter / compiler, you can't answer this question with anything else than assembly. – Martin Thoma Jan 5 '13 at 14:46
• The garbage collector uses recursion! So does the kernel of your OS. So, computers are not allowed. – boothby Aug 15 '13 at 17:20

# Python 2 (12)

>>> range(1,101)
[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, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]


# Ruby (11) [non-competitive]

p *(1..100)


(Thanks to histocrat)

Previous 14-character solution:

p *1.upto(100)


This is a non-competitive answer (not C/C++ as requested)

• p *(1..100) saves 3 characters. – histocrat Jan 5 '13 at 0:05
• @histocrat Thanks, I updated my answer. – knut Jan 5 '13 at 0:08
• Can't you remove the brackets too? – dingledooper Oct 1 '19 at 4:17

Scala (22)

1 to 100 foreach print

• @downvoter care to comment ? – lhk Dec 22 '15 at 10:51

# Perl 6 (10)

print ^101


If one wants to be able to read the numbers and therefore spaces between the numbers would be nice, the following will do the trick.

say ~ ^101


## Perl (65)

Here's a non-trivial Perl approach (not like say for 1 .. 100). The program uses perl's regular expression engine to count its own characters (two times), and that's the reason why I couldn't golfify the content of $k. It tries to find a (minimal, non-greedy) group of arbitrary single characters until (*FAIL). $k='$k =~ /^(.+?)(?{print length($1) . "\n"})(*FAIL)/#'x2;eval\$k;


The output is a list of all integers from 0 to 100 with newlines.

• Clever solution on many levels, nice. I don't understand the *FAIL though, is it some special string to the regex engine? If not, won't the * have its usual metacharacter meaning here and mess things up? – sundar - Reinstate Monica Aug 20 '13 at 18:49
• @sundar see the "Special Backtracking Control Verbs" section in perlre :) – memowe Aug 22 '13 at 0:39

# Pygmy, (18 characters)

alert|[1 100].fill


# Python 2 (39 chars)

p=lambda d:print d+1,;map(p,range(100))


# Python 3 (53 chars)

p=lambda d:print(d+1,end=' ');set(map(p,range(100)))


or print numbers on separate lines (28 char)

set(map(print,range(1,101)))


or (tested in Python 3.3.2, set items printing in ascending order - 24 chars)

print(set(range(1,101)))


## Bash 13

echo {1..100}


Some more text so it posts.

# JavaScript (ES6), 38 bytes

alert([...Array(101).keys()].slice(1))


Creates an array of length 101, uses the spread operator to put the array keys (0 to 100) into an array, removes the first element (0) then alerts the numbers within the array.

# Python - 61

Looping? No this is list comprehension.

d=[0,1,2,3,4,5,6,7,8,9];print[x*10+y+1 for x in d for y in d]

• That's stretching the "no hardcoding" pretty hard. – ugoren Aug 16 '13 at 7:02

# Hassium, 31 Bytes

func main()print(range(1, 100))


Run online and see expanded here

# JAVA, 52 chars

IntStream.range(1,101).forEach(System.out::println);

• Welcome to the site. I am not familiar with java but forEach looks an awful lot like a loop which seems to be forbidden by the challenge. – Post Rock Garf Hunter Feb 14 '17 at 21:42
• It's a stream function. It performs an operation on each element of the stream. It's not a loop. – aglassman Feb 15 '17 at 21:22
• I would disagree that a stream function is distinct from a loop but I don't think this argument is worthwhile. This question has been closed as unclear for the very reason that "loop" is not very well defined. – Post Rock Garf Hunter Feb 15 '17 at 21:25
• This challenge restricts to C/C++, so Java is probably unacceptable here. – Erik the Outgolfer May 15 '17 at 16:59