Euler zigzag numbers ( Alternating Permutation ) | Java program to find zigzag sequence ; Function to print first n zigzag numbers ; To store factorial and n 'th zig zag number ; Initialize factorial upto n ; Set first two zig zag numbers ; Print first two zig zag number ; Print the rest zig zag numbers ; Binomial ( n , k ) * a ( k ) * a ( n - k ) ; Store the value ; Print the number ; Driver code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void ZigZag ( int n ) { long [ ] fact = new long [ n + 1 ] ; long [ ] zig = new long [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) zig [ i ] = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; zig [ 0 ] = 1 ; zig [ 1 ] = 1 ; System . out . print ( " zig ▁ zag ▁ numbers : ▁ " ) ; System . out . print ( zig [ 0 ] + " ▁ " + zig [ 1 ] + " ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) { long sum = 0 ; for ( int k = 0 ; k <= i - 1 ; k ++ ) { sum += ( fact [ i - 1 ] / ( fact [ i - 1 - k ] * fact [ k ] ) ) * zig [ k ] * zig [ i - 1 - k ] ; } zig [ i ] = sum / 2 ; System . out . print ( sum / 2 + " ▁ " ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 10 ; ZigZag ( n ) ; } }