Java Program to Print ‘Hello World’ n times by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Factorial of a Number Using Recursion

In this program we are going to see how to print the “Hello Word” message n times using recursion by Java programming language. And print hello world n times in java, print hello world in java, how to print hello world in java, java program to print hello world, print hello world n times in python,
algorithm to print hello world, simple java program to print hello world, print hello world java, program to print hello world in java, you can easily do applications of java program to print Hello world.

Java Program to Print ‘Hello World’ n times by Using Recursion

Now let’s see different ways to print the “Hello World” message n times by using Recursion.

Method-1: Java Program to Print ‘Hello World’ n times By Using Static Input and Recursion

Approach:

  • Declare and initiate a static integer variable say count with the value of 0.
  • Declare and initiate an integer variable n and assign any value to it, which holds the value of number of times the message will be printed.
  • Call a user defined method printMessage() and pass n as parameter.
  • Inside the user defined method print the “Hello World” message by keeping a track on number of times the message is printed by using an If statement.
  • Call the same method inside that user defined method and print the message till specified n number of times.

Program:

class Main
{
    //Declare and initiate a static integer variable say count with the value of 0. 
    static int count=0;
    public static void main(String[] args)
    {
        int n=20;
        printMessage(n);
    }
    
    //define the method
    public static void printMessage(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the final n value reaches.
        if(count<=n)
        {
            //print the Hello world message 
            System.out.print("Hello World"+"\n");
             //call the same function recursively
            printMessage(n);
        }
    }
}
Output:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Method-2: Java Program to Print ‘Hello World’ n times By Using User Input and Recursion

Approach:

  • Declare and initiate a static integer variable say count with the value of 0.
  • Declare and initiate an integer variable n and prompt the user to enter the values to the corresponding variables by using Scanner class. The value of ‘n‘ holds the value of number of times the message will be printed.
  • Call a user defined method printMessage() and pass n as parameter.
  • Inside the user defined method print the “Hello World” message by keeping a track on number of times the message is printed by using an If statement.
  • Call the same method inside that user defined method and print the message till specified n number of times.

Program:

import java.util.Scanner;
class Main
{
    //Declare and initiate a static integer variable say count with the value of 0. 
    static int count=0;
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the total number of messages= ");
        //prompt the user to enter the value
        int n=sc.nextInt();
        //calling the method
        printMessage(n);
    }
    
    //define the method
    public static void printMessage(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the value 20.
        if(count<=n)
        {
            //print the Hello world message 
            System.out.print("Hello World"+"\n");
            //call the same function recursively
            printMessage(n);
        }
    }
}
Output:

Enter the total number of messages= 5
Hello World
Hello World
Hello World
Hello World
Hello World

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Answer these:

  1. Print name n times using recursion in java
  2. Understand recursion by print something n times
  3. Java code to print hello world
  4. Write a java program to print hello world

Also Refer: Perfect number in java – Java Program to Check Perfect Number by Using Recursion

Related Java Programs:

Conclusion

I hope you may like the content in this.