How to print 1 to n(a user defined value) without using any kind of loops or recursion ?
For C language, we can do this using setjmp and longjmp as given in the below code. Again there is an implicit loop, but no explicit looping construct or recursion used.
#include
#include
static jmp_buf jmpbuf;
static int val = 1, n;
void printvalue(void)
{
printf("%d\n",val++);
longjmp(jmpbuf,val);
}
int main()
{
printf("Enter the N value:");
scanf("%d",&n);
if (setjmp(jmpbuf) > n)
return 0;
else
printvalue();
}
#include
static jmp_buf jmpbuf;
static int val = 1, n;
void printvalue(void)
{
printf("%d\n",val++);
longjmp(jmpbuf,val);
}
int main()
{
printf("Enter the N value:");
scanf("%d",&n);
if (setjmp(jmpbuf) > n)
return 0;
else
printvalue();
}
0 comments:
Post a Comment