» » Print 1 to 10 without using loop in java?

  •  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:


  1. package com.instanceofjavaTutorial;
  2.  
  3. class Demo{  
  4.  
  5. public static void main(String args[]) { 

  6.    System.out.println(1);
  7.    System.out.println(2);
  8.    System.out.println(3);
  9.    System.out.println(4);
  10.    System.out.println(5);
  11.    System.out.println(6);
  12.    System.out.println(7);
  13.    System.out.println(8);
  14.    System.out.println(9);
  15.    System.out.println(10);
  16.  
  17. }

  18. }

Output:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10



Solution #2


  1. package com.instanceofjavaTutorial; 
  2. class PrintDemo{
  3.  
  4. public static void recursivefun(int n) 
  5.  
  6.   if(n <= 10) {
  7.  
  8.        System.out.println(n); 
  9.          recursivefun(n+1);   }
  10. }
  11.  
  12. public static void main(String args[]) 
  13. {
  14.  
  15. recursivefun(1); 
  16.  
  17.  }
  18.  
  19. }




Output:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu