Java Program to Print 1 to 50 without Using Loop

In the previous article, we have seen Java Program to Find Cube of a Number

In this article we will see how to print 1 to 50 without using loop in Java programming language.

Java Program to Print 1 to 50 without Using Loop

Take an integer variable say number and initialize it to 1. We will take one method say displayNumber() inside that method we will take one if condition and we will check if number<50 then we will print the current number and we will call the method displayNumber() recursively by passing number+1 as passing as parameter.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //declaring an integer variable 'number' 
        //and initializing it 1
        int num = 1;
        
        //user defined method displayNumber()
        System.out.print("Printing numbers: "); 
        displayNumber(num);	
    }
    
    //displayNumber() to print from 50 to 100
    public static void displayNumber(int num)
    {
        if(num <= 50)
        {
            System.out.print(num +" "); 
            //calling displayNumber() method recursively
            displayNumber(num + 1);
        }	
    }
}
Output:

Printing numbers:  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

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: