Computer Science, asked by thebeast1123N, 2 months ago

) void pattern (String s) – accepts a string argument and displays the pattern as

follows:

For Example: s="school"

Output:
s
sc
sch
scho
schoo
school

Answers

Answered by Oreki
1

Method

   void pattern(String string) {

       for (int i = 0; i < string.length( ); i++)

           System.out.println(string.substring(0, i + 1));

   }

Algorithm

  • Printing subsequent substrings of the passed string using a for loop.
Attachments:
Answered by anindyaadhikari13
4

Required Answer:-

Solution:

This is the required program for this question.

public class Pattern {

 public static void main(String[] args) {

     Pattern obj=new Pattern();

     obj.pattern("SCHOOL");

 }

 void pattern(String s) {

    for(int i=0;i<s.length();i++)

      System.out.println(s.substring(0, i+1));

 }

}

Explanation:

  • void pattern(String s):- This function accepts a string and display the pattern.
  • Object of pattern class is created and the function pattern() is called.

Refer to the attachment for output ☑.

Attachments:
Similar questions