How to print numbers ranging from 0 without using looping statement

Codynn
4 Min Read

Generally, we have to use looping statement like while loop, for loop, do while loop etc. to print numbers ranging from 0 to numbers specified. We have requirement that does not gives us right to use looping statements. These type of question is mostly asked in coding interview to check coding knowledge or in job interview to hire.

We can solve this problem with recursion. In general term recursion means to call itself again and again. When a program requires you to access a function within another function, this is referred to as a recursive call of the function.

First we have to make a class and main method. Then initialize a variable and assign the value 1 to that variable. Now our plan is to call main method again and again. So, write the variable outside the main method and just after the class. After that in main method, we call the main method inside main method which we call by the term as recursion. Since we are trying to print from one to fifty, we will check whether the value is less than or equal to 50. Inside if condition, print value with one increase.

class Test 
 {
     static int var = 1;
 public static void main(String[] args)  {    
 if (var <= 50){                
 System.out.println(var++);         
  main(args);   
               }
        }
 }

We are calling main method here again and again until the value becomes equal to 50.

You can also ask the user about how many times they want to loop to work starting from 0. For this we can use Scanner class. If we write inside the main method, it asks every time user input. So, we have to ask the user outside the main method as:

import java.util.Scanner;
 class Test 
 {
 static int var = 1;
 static Scanner sc = new Scanner(System.in); 
static int lastnumber = sc.nextInt(); 
public static void main(String[] args)  {    
 if (var <= lastnumber)     {       
      System.out.println(var++);      
      main(args);    
          } 
      }
 }

We asked the user number of loops they want to print and stored that number in lastnumber. However the disadvantage of this program is we cannot display the text to user asking about the number of loop they want to make after scanner class.

We cannot write code like this.

import java.util.Scanner;
 class Test 
 {
 static int var = 1;
 static Scanner sc = new Scanner(System.in); 
System.out.print("Enter the number of loop");
static int lastnumber = sc.nextInt(); 
public static void main(String[] args)  {    
 if (var <= lastnumber)     {       
      System.out.println(var++);      
      main(args);    
          } 
      }
 }

It is due to the fact that we cannot print something outside the main method in java.

To solve this problem we can define another method, there we can call that method inside the same method instead of main method just like below.

import java.util.Scanner;
 class Test 
 {
static void number(int var) { 
    if(var > 0)     
{   
      number(var--);    
     System.out.print(var + " ");   
     }
 } 
 public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter the number of loops"); 
    int lastnumber = sc.nextInt();
     number(lastnumber); 
}

Here we defined number method with one parameter var and inside same method we called number method with one decrement operator for var. You can also use var= var-1 instead of var–.

You can also use println according to your requirements.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *