Repeat word as many times as it has characters


 

Repeat word as many times as it has characters

This section illustrates you the repetition of same word as many times as it has characters in it.

This section illustrates you the repetition of same word as many times as it has characters in it.

Repeat word as many times as it has characters in Java

This section illustrates you the repetition of same word as many times as it has characters in it. To do this, we prompt the user to enter the word using the Scanner class. After finding the length of the word, we have created a for loop, it will display the entered word no of times its length.

Here is the code:

import java.util.*;
class RepeatWord{
  public static void main(String[] args)  {
  System.out.println("Enter word");
  Scanner input=new Scanner(System.in);
  String word=input.next();
  int len=word.length();
    System.out.println();
  for(int i=0;i<len;i++){
    System.out.println(word);
  }
  }
}

Output:

Enter word
Hello

Hello
Hello
Hello
Hello
Hello

Ads