Yet another fun C-puzzle
-
Good code, but C does not support templates :-) Nish
:-O i got carried away by this line from your original post: "An answer was later posted in C++ (and the poster said that it was not possible without C++)."
-
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr -
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason
-
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yrif
is a conditional statement - not allowed. -
BTW 100
printf
s don't count :rolleyes:Does one
printf
with 100 numbers count? :) sorry if this question was already asked, I didn't look at other posts... David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yrutsav_verma wrote: int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? In addition to "if" being conditional (and therefore you break the rules), there are other bugs in your code. The first argument to main is the number of arguments and if you invoke the prog as "filename 0" as you say, this will be equal to 2 (including the file name). Thus your prog starts printing from 2. 2nd issue is that main should ideally take 2 arguments (though most compilers will let your code compile).
-
Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason
Jason Pease wrote: Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } :rolleyes:
-
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
#define max 100 static int i = 1; bool Print() { print(i++); return ((max - i) && Print()) }
Since we cant use iterative approarch (loops) let's make it recursive. We can't use condition statement (if) so let's use short-circuit expression :) yeah quite late but I had to go visit lectures :( I've solved it on the way there :) David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
#define max 100 static int i = 1; bool Print() { print(i++); return ((max - i) && Print()) }
Since we cant use iterative approarch (loops) let's make it recursive. We can't use condition statement (if) so let's use short-circuit expression :) yeah quite late but I had to go visit lectures :( I've solved it on the way there :) David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidyWell thank you wery much David for enlightening me with your approach to the problem!!! - Finally I've got it what the last line (like in Vivek Ryan's code, for example) actually does! I persume, now, that C doesn't run/check the second argument of AND(&&) when the first is already evaluated as "bogus". Nice trick, must say! MY GREETINGS TO ALL by mladjan
-
Here's an improvement (LOC reduced anyway)
int main(int ac, char ** av)
{
printf("%d ",101-ac,(ac%100)&&main(ac+1,0));
}Run the app without arguments (first time ac will be 1) Nish