text: Program to print Step Pattern | Java Program to print Step Pattern ; function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Driver code ; Get the String and the number n ; calling the function
code: import java . util . * ; class solution { static void steps ( String str , int n ) { boolean flag = false ; int x = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( x == 0 ) flag = true ; if ( x == n - 1 ) flag = false ; for ( int j = 0 ; j < x ; j ++ ) System . out . print ( " * " ) ; System . out . print ( str . charAt ( i ) + "NEW_LINE"); if ( flag == true ) x ++ ; else x -- ; } } public static void main ( String args [ ] ) { int n = 4 ; String str = " GeeksForGeeks " ; System . out . println ( " String : ▁ " + str ) ; System . out . println ( " Max ▁ Length ▁ of ▁ Steps : ▁ " + n ) ; steps ( str , n ) ; } }