can any one help me in solving a question posted by friend.....
i tried to google but i didnt find the accurate answer for my question..
i wish to 'print 1 to n' without using loops and recursion

There was a similar question that become popular on SO recently. That version of the problem didnt allow loops or conditionals so the answers are mostly recursion based and n happened to be fixed. If n is user supplied some of the standard answers would be excluded (or need modification).

WHat have you attempted so far? What are your thoughts on how to approach to problem?

i too thought of doing the code using recursion or loops but the question is not to use any of those.
and most of the ans are based on goto label which is also a loop.

You still haven't stated whether your range is fixed or dynamic. It makes a significant difference in the difficulty of the problem.

yes i want the value to be dynamic
it should be the wish of the user who executes the program..

So you want to print an unknown, variable range of numbers, without using any form of loop or recursion. Pretty much any solution with those restrictions will rely on a hidden loop or recursion at a deeper level. It becomes a question of semantics: "Is this enough of a loop to fall under the restriction?"

Depending on how the question is answered, the problem could be solved with a trick, or it could be impossible.

sorry sir i didnt get you

i want the question to be solved without any recursion or looping concept for unknown variable range of numbers..

i want the question to be solved without any recursion or looping concept for unknown variable range of numbers..

You don't need to repeat the requirements, you need to clarify them. For example, if I write a program that calls another pre-existing program to do the looping, does that still count as using a loop? If I simulate recursion using signals, threads or by spawning new processes, does that still count as recursion?

sir i am a beginner in c and i dont know about signals and those
calling a function and writing a loop there is to be considered as using a loop in my view sir..
i dont wish like that

There has to be a loop or recursion in some form somewhere. Since you clearly aren't able to refine your description of the problem to allow for semantic loopholes, the problem is impossible. It cannot be solved on a conventional computer.

so isnt it possible to print 1 to n numbers where 'n' is dynamically given by user without loops and recursion???

My guess is you need a loop somewhere...

When you say print from 1 to n it either has to be a fixed N or until forever?

One way to do this (and I may be completely off here) is to print n+=1 from now until infinity using the computer's clock, but even then, how do you control it? And how do you implement such a function?

It'd have to be something like:

``void printNplus1(int *n){ n+=1; printf(%d, n); }``

and call that every second...

But to call this function you would still need to create a control for it, without loop... If this is possible, it would be definitely cool to see the answer.

Of course N has to be larger than an integer if it's to be infinite. And you would have to pass it as a pointer so that the variable gets updated every time. But that still doesn't solve the control problem.

My 2c.

For me this is only way (OS uses of course loop):

``````#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
char range[298] = "  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";
printf("Number between 1 and 99: ");
scanf("%2d", &n);
if(n > 99 && n < 1) {
printf("Not between one and ninety-nine");
} else {
range[3*n] = 0;
printf(range);
return 1;
}
return 0;
}``````

hey tonyjv i want the program to print till 'n' which is to be given by the user.
that means user has to enter the value of 'n' at runtime..

so can we conclude that there is no chance to do this task without a loop or a recursion?

is goto a control statement..??
its a branching statement and
is it a control statement too??

My program takes input from user at line 9, but has limited range. It can be of course be extended longer with bigger string and more than two digits scanf. i did mix the return values. Zero should be returned on success, not on failure.

I got almost all the syntax wrong, as I am a Python guy, so it should be || for range conditions, and input can not be limitted to 2 numbers if too big numbers or negative ones are to be checked. You should add checking of returned value from scanf.

``````#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
char range[298] = "  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";
printf("Number between 1 and 99: ");
scanf("%d", &n);
if(n > 99 || n < 1) {
printf("Not between one and ninety-nine");
} else {
range[3*n] = 0;
printf(range);
return 0;
}
return 1;
}``````

yeah thanks i got ur code
thanks a lot

is this correct range[3*n] = 0;
or we have to use range[n+1]='\0';

so can we conclude that there is no chance to do this task without a loop or a recursion?

We can conclude that only because you've completely failed to be clear about what constitutes a loop or recursion. The usual solution is to simulate a loop with goto:

``````#include <stdio.h>

int main(void)
{
int limit;

printf("Enter a limit: ");
fflush(stdout);

if (scanf("%d", &limit) == 1) {
int i = 1;

again:
if (i < limit) {
printf("%d\n", i++);
goto again;
}
}

return 0;
}``````

This depends on the loophole that "loop" means one of the explicit loop constructs: while, do..while, and for. While goto can be used for circular jumps (looping), that's not its only capability, which potentially excludes it from the restrictions.

Another solution uses looping/recursion hidden behind a library or by spawning threads/processes to simulate recursion. However, all of these can be labeled as loops and recursion depending on how strict you are, so a very specific definition of the program's restriction is needed before any of them can be called a solution.

Try it out and see, why it is so, '\0' could be used of course instead of 0.

If goto would be allowed the wording should be specific along the lines:

"not using for, do..while or while loop constructs or recursion"

ok
thanks.
and if i should not even use a goto statement..
what might be the alternative..??
i mean no recursion no iteration and no goto..
and not like the logic given by tonyjv..

and if i should not even use a goto statement..
what might be the alternative..??

Are you reading impaired? I've listed several alternatives, and they STILL depend on how strict your definition of iteration and recursion are. Stop asking the same damn question when you need to be more specific. Answer the following questions:

• If I recursively spawn new threads or processes to do the work, does that count as recursion?
• If I call a library that performs a loop to do the work, does that count as a loop?
• If I call another pre-existing program that does the work in an unspecified manner, does that still count as a loop or recursion?
commented: Give it up, they ain't worth the effort +17

As your program is not the only program running in system, but thin abstraction over the machinery of computer.

Most of this stuff is really over your head as you stated to be beginner even if I and especially Narue can deal with those. For example it is simple to record current time to file. Do cron job repeating every second and read the starting time from file (preferably ticks since epoch) and calculate passed second since start. Stop doing this at user given number of seconds recorded in same file.

sorry
i hope its indirectly a loop
ok i got it
i understood and thanks for ur reply

I don't sure but try it.

``````#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
clrscr();
printf("\nEnter the value for n:");
scanf("%d",&n);

if(i!=n)
{
printf("%d ",i);
i=i+1;
continue;
}
getch();
}``````
commented: rubbish - won't even compile -4

can I use c++

yes u can use..
but if possible try in c too

thanks muthmuth
but i think continue can be used only with loops

With C++ this becomes trivial. Use a constructor with a static member. For instance:

``````class Printer {
static int count_;
public:
Printer () { std::cout << ++count_ << std::endl; }
};``````

Does exactly what you need. (It does, however, employ some looping mechanism at some level).

C does not have the constructor concept. Structs are just POD so this type of trick is not available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.