How to print 1 to 100 numbers without using loops concepts (for/while/do-while) ?
This is a tricky question. Think Think Think.
..
..
..
..
Hint: We can use the concept of static variables to do this task.
Think
..
..
..
Here is the program, which solves the problem.
x = 12
y = 6
x should be less than or equal to y.
This is a tricky question. Think Think Think.
..
..
..
..
Hint: We can use the concept of static variables to do this task.
Think
..
..
..
Here is the program, which solves the problem.
- #include<stdio.h>
- int main()
- {
- static int i = 0;
- printf("%d\n", i++);
- if (i<=100)
- main();
- return 0;
- }
Now, we can made a small change in the program to display numbers from x to y;
Here is the program to do this task.
- #include<stdio.h>
- int main()
- {
- static int i = 0, x ,y;
- if (i == 0) {
- printf("Please enter two numbers, x should be <= y\n");
- printf("x = ");
- scanf("%d", &x);
- printf("y = ");
- scanf("%d", &y);
- if (x > y) {
- printf("x should be less than or equal to y.\n");
- return 0;
- }
- }
- printf("%d\n", x+(i++));
- if ((x+i)<=y)
- main();
- return 0;
- }
Example output:
Please enter two numbers, x should be <= y
x = 12
y = 17
12
13
14
15
16
17
Example output_2:
Please enter two numbers, x should be <= yx = 12
y = 6
x should be less than or equal to y.
No comments:
Post a Comment