Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java program to print number series without using any loop
In this article, we will learn how to print a sequence of numbers in Java, ranging from 0 to 15. To do this, we'll use recursion rather than using loops like for loop or while loop.
Recursion is a programming technique where a method calls itself to perform a sub-operation as necessary.
Problem Statement
Write a Java program to print Number series without using any loop
Output
The numbers without using a loop have been printed below
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
Java program to print number series without using any loop
Following is the Java code to print Number series without using any loop ?
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 a loop have been printed below 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
Code Explanation
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 the condition is checked if the number passed is 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.
