Program to print the pattern 1020304017018019020 * * 50607014015016 * * * * 809012013 * * * * * * 10011. . . | Java implementation to print the given pattern ; Function to find the sum of N integers from 1 to N ; Function to print the given pattern ; Iterate over [ 0 , N - 1 ] ; Sub - Pattern - 1 ; Sub - Pattern - 2 ; Count the number of element in rows and sub - pattern 2 and 3 will have same rows ; Increment Val to print the series 1 , 2 , 3 , 4 , 5 ... ; Finally , add the ( N - 1 ) th element i . e . , 5 and increment it by 1 ; Initial is used to give the initial value of the row in Sub - Pattern 3 ; Sub - Pattern 3 ; Skip printing zero at the last ; Driver code ; Given N ; Function call
import java . util . * ; class GFG { static int sum ( int n ) { return n * ( n - 1 ) / 2 ; } static void BSpattern ( int N ) { int Val = 0 , Pthree = 0 , cnt = 0 , initial = - 1 ; String s = " * * " ; for ( int i = 0 ; i < N ; i ++ ) { cnt = 0 ; if ( i > 0 ) { System . out . print ( s ) ; s += " * * " ; } for ( int j = i ; j < N ; j ++ ) { if ( i > 0 ) { cnt ++ ; } System . out . print ( ++ Val ) ; System . out . print ( "0" ) ; } if ( i == 0 ) { int Sumbeforelast = sum ( Val ) * 2 ; Pthree = Val + Sumbeforelast + 1 ; initial = Pthree ; } initial = initial - cnt ; Pthree = initial ; for ( int k = i ; k < N ; k ++ ) { System . out . print ( Pthree ++ ) ; if ( k != N - 1 ) { System . out . print ( "0" ) ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 5 ; BSpattern ( N ) ; } }