- Basically to display numbers from 1 to 10 or a series we will use for , while or do while loop
- So here is the programs to do same thing without using loop.
- This is possible in two ways
- First one is to display by printing all those things using system.out.println.
- second one is by using recursive method call lets see these two programs
Solution #1:
- package com.instanceofjavaTutorial;
- class Demo{
- public static void main(String args[]) {
- System.out.println(1);
- System.out.println(2);
- System.out.println(3);
- System.out.println(4);
- System.out.println(5);
- System.out.println(6);
- System.out.println(7);
- System.out.println(8);
- System.out.println(9);
- System.out.println(10);
- }
- }
Output:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Solution #2
- package com.instanceofjavaTutorial;
- class PrintDemo{
- public static void recursivefun(int n)
- {
- if(n <= 10) {
- System.out.println(n);
- recursivefun(n+1); }
- }
- public static void main(String args[])
- {
- recursivefun(1);
- }
- }
Output:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
No comments