Java Program to print Number series without using any loop

JavaObject Oriented ProgrammingProgramming

Following is the Java code to print Number srries without using any loop −

Example

 Live Demo

public class Demo{
   public static void main(String[] args){
      int my_num = 0;
      System.out.println("The numbers without using loop have been printed below");
      print_without_loop(my_num);
   }
   public static void print_without_loop(int my_num){
      if(my_num <= 15){
         System.out.print(my_num +",");
         print_without_loop(my_num + 1);
      }
   }
}

Output

The numbers without using loop have been printed below
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

A class named Demo contains the main function where a variable named ‘my_num’ is initialized to 0. A function named ‘print_without_loop’ is called. It is defined further wherein condition is checked if the number passedis greater than 15, if yes, then the number is printed beginning from 0 and incrementing it after every pass. Once the element 15 is reached, the condition exits.

raja
Published on 07-Jul-2020 11:13:15
Advertisements