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

write a program to display all prime numbers

0 Answers  


What are nested functions in c?

0 Answers  


main() { charx; while (x=0;x<=255;x++) printf("\nAscii value %d Character %c,x,x); }

2 Answers  


21. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); }

3 Answers  


The __________ attribute is used to announce variables based on definitions of columns in a table?

0 Answers  






What is meant by high-order and low-order bytes?

0 Answers  


What is getche() function?

0 Answers  


What is #define in c?

0 Answers  


When should a far pointer be used?

0 Answers   Aspire, Infogain,


a simple program in c language

5 Answers   IBM,


34.what are bitwise shift operators? 35.what are bit fields? What is the use of bit fields in a structure declaration? 36.what is the size of an integer variable? 37.what are the files which are automatically opened when a c file is executed? 38.what is the little endian and big endian? 39.what is the use of fflush() function? 40.what is the difference between exit() and _exit() functions? 41.where does malloc() function get the memory? 42.what is the difference between malloc() and calloc() function? 43.what is the difference between postfix and prefix unary increment operators?

3 Answers  


what is the difference between %d and %*d in c languaga?

7 Answers   TCS,


Categories