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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
The variables are int sum=10,SuM=20; these are same or different?
Explain what is the benefit of using enum to declare a constant?
What is strcmp in c?
What are terms in math?
Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?
Differentiate call by value and call by reference?
What is the advantage of a random access file?
Give the logic for this #include<stdio.h> #include<conio.h> void main() { clrscr(); int a=10,b; b=++a + ++a; printf("%d", b); getch(); } Output: 24......How?
What is the difference between malloc() and calloc() function in c language?
How is a two dimensional array passed to function when the order of matrix is not known at complie time?
What is s in c?
When do we get logical errors?