Write a C++ program without using any loop (if, for, while
etc) to print numbers from 1 to 100 and 100 to 1;

Answers were Sorted based on User's Feedback



Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / manish verma

I guess, if it is not asking for any kind of loop (for /
while) then one can use recursive functions to achieve this.

Is This Answer Correct ?    63 Yes 9 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / om

#include<stdio.h>
void print_1_to_100(int n);
void print_100_to_1(int n);

int main()
{
print_1_to_100(1);
return 0;
}

void print_1_to_100(int n)
{
printf("%d\t",n);
(n/100)? print_100_to_1(n) :print_1_to_100(n+1);
}


void print_100_to_1(int n)
{
printf("%d\t",n);
(n-1)? print_100_to_1(n-1) :1;
return;
}

//SAMPLE OUTPUT
1 2 3 4 ....100 100 99 98 ...2 1

Is This Answer Correct ?    52 Yes 17 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / sandy

#include<iostream>
int i;
class A
{
public:
A(){cout<<i++<<endl;}
~A(){cout<<--i<<endl;}
}
int main()
{
A a[100];
return 0;
}

Is This Answer Correct ?    41 Yes 10 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / kt vikram

We are using 'goto' statement to print the numbers without using
if,for,while etc.

Is This Answer Correct ?    38 Yes 23 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / raja dt

int main(int argc, char* argv[])
{
static int i=1, j=100;
(i==101)? i=i: main(printf("%d ", i++), NULL);

(j==0)? j=j: main(printf("%d ", j--), NULL);
return 0;
}

// Output
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 100 99 98 97 96 95
94 93 92 91 90 89 88 87 86 85 84 83 82 81 80
79 78 77 76 75 74 73 72 71 70 69 68 67 66 65
64 63 62 61 60 59 58 57 56 55 54 53 52 51 50
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35
34 33 32 31 30 29 28 27 26 25 24 23 22 21 20
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
2 1

Is This Answer Correct ?    10 Yes 7 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / ajeet kumar

#include<stdio.h>
#include<conio.h>

int f(int);

void main()
{
static int n=0;

n++>99 ? 0 : f(n) ; //its value are not used.
}

int f(int n)
{
printf(" %d",n);
main();
printf(" %d",n--); //]internally stack is ctr
return(1);
} /* run firstly,after press alt+F5.

Is This Answer Correct ?    4 Yes 2 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / jihad mzahim

int main()
{
int i=100;
start:
if(i==0)
{
printf("\t\t************************************************\n\n");
goto start1;

}

printf("%d\t",i);
i--;
goto start;

start1:
if(i==101)
goto end;
printf("%d\t",i);
i++;
goto start1;

end:
printf("\n end of the program");

return 0;

}

Is This Answer Correct ?    3 Yes 4 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / raveendra

#include<iostream.h>
using namespace std;

int i;
class A
{
public:
static void fun()
{
A a[100];
}
A(){cout<<++i<<"\t";}
~A(){cout<<i--<<"\t";}
};

int main()
{
A::fun();
getchar();
return 0;

}

Is This Answer Correct ?    1 Yes 3 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / shubham gupta

#include<stdio.h>
void fun(int n)
{
static int i=1,j;
j=printf("%d\n",i); // when i=100 printf will return 4
// bcoz of 3 difits of 100 and 1 '\n' character
switch(j)
{
case 4:
exit(0);
default: i++;
fun(n) ;
}

}
int main()
{
int n;
n=100;
fun(n);
}

Is This Answer Correct ?    0 Yes 6 No

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and..

Answer / shubham gupta

#include<stdio.h>
void fun(int n)
{
static int i=1,j;
printf("%d\n",i);
(100-i)?(i++?fun(n):0):exit(0);

}
int main()
{
int n;
n=100;
fun(n);
}

Is This Answer Correct ?    1 Yes 7 No

Post New Answer

More C Interview Questions

How the C program can be compiled?

11 Answers   HP,


what is array?

63 Answers   Amdocs, HCL,


what is the output of printf("%d",(scanf("%d",10));

10 Answers  


What is the difference between macros and inline functions?

5 Answers   Global Edge, L&T,


wat is the meaning of c?

9 Answers   CTS, IBM, Wipro,






What is a structural principle?

0 Answers  


what will be the output off the following program? #include<stdio.h> int main() { int a; a=015+0*71+5; printf("%d,a"); return0; }

9 Answers   HCL,


Explain modulus operator. What are the restrictions of a modulus operator?

0 Answers  


with out using main how to execute the program?

2 Answers  


What is chain pointer in c?

0 Answers  


How to reverse a string using a recursive function, without swapping or using an extra memory?

31 Answers   Cisco, Mind Tree, Motorola, Ophio, Sony, TCS, Wipro,


What is the advantage of using #define to declare a constant?

0 Answers   Agilent, ZS Associates,


Categories