Zig-Zag Pattern in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main(){
    int n=9;
    for(int i=1;i<=3;i++){
        for(int j=1;j<=n;j++)
            printf(( (i+j)%4==0 || (i==2 && j%4==0) ) ? "*" : " ");
        printf("\n");
    }
}

C Output

Input:  
n = 9 

Output:  
  *   *   *
 * * * * * 
*   *   *  


C++ Program

#include <iostream>
using namespace std;
int main(){
    int n=11;
    for(int i=1;i<=3;i++){
        for(int j=1;j<=n;j++)
            cout<<(((i+j)%4==0)||(i==2&&j%4==0)?"*":" ");
        cout<<"\n";
    }
}

C++ Output

Input:  
n = 11

Output:  
  *   *   *   *
 * * * * * * * 
*   *   *   *  


JAVA Program

public class Main{
    public static void main(String[] args){
        int n=7;
        for(int i=1;i<=3;i++){
            for(int j=1;j<=n;j++)
                System.out.print(( (i+j)%4==0 || (i==2 && j%4==0) )?"*":" ");
            System.out.println();
        }
    }
}

JAVA Output

Input:  
n = 7 

Output:  
  *   * 
 * * * *
*   *   


Python Program

n=13
for i in range(1,4):
    print(''.join('*' if (i+j)%4==0 or (i==2 and j%4==0) else ' ' for j in range(1,n+1)))

Python Output

Input:  
n = 13  

Output:  
  *   *   *   * 
 * * * * * * * *
*   *   *   *   


Deep-Dive Explanation
Learning the Zig-Zag Pattern
The zig-zag pattern produces a wave motion effect in text by placing stars (*) in specific locations in a 3-row grid. Rather than printing in straight lines, the stars go diagonally in alternating positions, creating the illusion of a zig-zag or sine wave.

Example
For n = 9, the position of the stars has a predictable pattern:

In row 1, stars occur at (i+j) % 4 == 0.

In row 2, stars show up in two locations: where j % 4 == 0 and where (i+j) % 4 == 0.

In row 3, stars again show up where (i+j) % 4 == 0.

This formula makes the stars "move" diagonally.

Real-Life Analogy
Imagine Christmas lights going from side to side on a wall or along the path of a snake slithering in waves. The stars resemble light bulbs, and the spaces are between them.

Why It Matters
This is a great exercise in understanding conditional placement with nested loops. It's not so much about printing rows and columns evenly — it's about exact placement according to mathematical rules, and that's extremely valuable in graphics, print algorithms, and game development.

Learning Insights
This instructs beginners:

How to apply modulo arithmetic to position patterns.

How to conceptualize patterns as math functions, rather than loops.

How to escape left-to-right, top-to-bottom thinking and embrace diagonal logic instead.

Interview & Real-World Use
In interview, this is an example of whether you can convert a graphic pattern into a code using arithmetic conditions. In real-world usage, the same principle applies for graphics rendering, LED timing, or wave animation.

The C, C++, Java, and Python zig-zag pattern program is an ideal demonstration of nesting loops with conditional statements to create visually pleasing output. By learning this, developers can create wave-like effects, sine-wave effects, and graphics based on text, and it remains a popular choice in programming practice, interview questions, and creative console art.