Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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);

share|improve this question
    
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
3  
Does goto count? –  minitech Dec 31 '12 at 0:50
    
Do preprocessor conditions count? –  ValekHalfHeart Jan 16 at 21:50
add comment

20 Answers

up vote 16 down vote accepted

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

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, but I exploited a buffer overflow attack and change the return address to make program counter go over function a again as long as I need.

I don't know if it 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.

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;}
share|improve this answer
    
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 Aug 17 '13 at 9:50
1  
@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  
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
1  
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 Aug 19 '13 at 18:29
show 7 more comments

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.

share|improve this answer
3  
Using the preprocessor is almost like hard coding it. –  moose Jan 1 '13 at 16:52
    
agreed. This is hardwired. –  Cong Hui Nov 9 '13 at 8:01
add comment

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>();}
share|improve this answer
2  
s/class/struct/;s/public://;s/static //;s/::/()./g saves 11 characters. –  leftaroundabout Dec 30 '12 at 19:49
add comment

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++);)))}
share|improve this answer
    
? 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
add comment

MATLAB/Octave, (5 chars)

1:100
share|improve this answer
3  
Doesn't the question want a code in C? –  saeedn Aug 15 '13 at 21:40
add comment

Python 3 (25)

print(list(range(1,101)))
share|improve this answer
    
it is a inbuilt function that uses recursion! –  Pranit Bauva Jan 5 '13 at 14:21
5  
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. –  moose Jan 5 '13 at 14:46
5  
The garbage collector uses recursion! So does the kernel of your OS. So, computers are not allowed. –  boothby Aug 15 '13 at 17:20
add comment

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]
share|improve this answer
add comment

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;}
share|improve this answer
add comment

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)

share|improve this answer
2  
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
add comment

Scala (22)

1 to 100 foreach print
share|improve this answer
add comment

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
share|improve this answer
add comment

C++ (115)

#include <cstdio>
template<int i>void p(){printf("%d ",i);p<i+1>();}
template<>void p<101>(){}
int main(){p<1>();}
share|improve this answer
    
Isn't this recursion? –  Bryan Chen Sep 18 '13 at 5:47
add comment

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.

share|improve this answer
    
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 Aug 20 '13 at 18:49
1  
@sundar see the "Special Backtracking Control Verbs" section in perlre :) –  memowe Aug 22 '13 at 0:39
add comment

Pygmy, (18 characters)

alert|[1 100].fill
share|improve this answer
add comment

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]
share|improve this answer
    
That's stretching the "no hardcoding" pretty hard. –  ugoren Aug 16 '13 at 7:02
add comment

Haskell (23)

main=mapM print[1..100]

Obviously not C/C++.

share|improve this answer
add comment

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)))
share|improve this answer
add comment

Bash 13

echo {1..100}

Some more text so it posts.

share|improve this answer
add comment

Mathematica (9)

Range@100

You can check it on http://www.mathics.net/

share|improve this answer
add comment

PowerShell (6 characters)

1..100

share|improve this answer
    
Equally doable, though no shorter, with 1..1e2. –  Iszi Nov 16 '13 at 16:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.