Tell me more ×
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 – Lonely baby Dec 27 '12 at 7:21
2  
Does goto count? – minitech Dec 31 '12 at 0:50

12 Answers

up vote 6 down vote accepted

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
1  
s/class/struct/;s/public://;s/static //;s/::/()./g saves 11 characters. – leftaroundabout Dec 30 '12 at 19:49

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
1  
Using the preprocessor is almost like hard coding it. – moose Jan 1 at 16:52

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 at 14:40
vignesh4303 disallowed "if conditions", not all conditionals, which is why I included both answers. – cardboard_box Jan 11 at 2:24

MATLAB/Octave, (5 chars)

1:100
share|improve this answer

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

Scala (22)

1 to 100 foreach print
share|improve this answer

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
1  
p *(1..100) saves 3 characters. – histocrat Jan 5 at 0:05
@histocrat Thanks, I updated my answer. – knut Jan 5 at 0:08

Pygmy, (18 characters)

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

Python 3 (25)

print(list(range(1,101)))
share|improve this answer
it is a inbuilt function that uses recursion! – Pranit Bauva Jan 5 at 14:21
1  
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 at 14:46

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

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

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

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.