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

Is anything faster than c?

0 Answers  


Can you explain the four storage classes in C?

0 Answers   TCS,


Explain main function in c?

0 Answers  


Write a C Program to display the following menu: Menu 1. Display 2. Copy 3. Append 4. Exit Accept the choice (1-4) from the user, and perform the following tasks: Choice 1: Accept a file name from the user and display the file on screen Choice 2: Accept two file names, and copy first file to the second Choice 3: Accept two file names, and append second file to the first file Choice 4: Terminate the program

1 Answers   Accenture, Concor, DMU, Satyam, Syntel, Tora,


which is faster execution: loops or recursion?

3 Answers  






Who invented b language?

0 Answers  


How many bytes are occupied by near, far and huge pointers (dos)?

0 Answers  


what is the difference between <stdio.h> and "stdio.h"

14 Answers   Invendis, Kanbay, Mastek, MathWorks,


Can we write a program without main() function?

9 Answers  


Not all reserved words are written in lowercase. TRUE or FALSE?

0 Answers  


Write a C++ program to give the number of days in each month according to what the user entered. example: the user enters June the program must count number of days from January up to June

0 Answers  


Q. where is the below variables stored ? - volatile, static, register

3 Answers   Bosch,


Categories