Java Program to Print Zig Zag Character Pattern

Program to Print Zig Zag character Pattern

In the previous article, we have discussed Java Program to Print Heart Character Pattern

In this article we will see how to print zig-zag character pattern.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach :

  1. Print one backward slash first then one forward slash and continue.
  2. Enter total characters in a row and store it an integer variable say row.
  3. Enter total no. of zig zag lines and store in an integer variable say count.
  4. To print Backward slash :
  5. Take first for loop to print all the rows.
  6. Take a inner loop to print the column values.
  7. Then go on printing the characters according to the iteration.
  8. To print forward slash :
  9. Take first for loop to print all the rows.
  10. Take inner loop to print the column values.
  11. Then go on printing the characters according to the iteration.

Java Code to Print Zig Zag Character Pattern

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        // Take user input for no fo rows 
        System.out.print("Enter the no of characters in a line = ");
        Scanner sc= new Scanner(System.in);
        int r,c,row;
        //starting ASCII value taken 64
        int ascii=64;
        // store the input value in row
        row=sc.nextInt();
         System.out.print("Enter the no of zig zag line = ");
        int count=sc.nextInt();
        
        for (int i=1;i<=count;i++)
        {
        
            // backward
           for(r=1; r<=row; r++)
           {   
               // inner loop to print symbol
              for(c=1; c<=row; c++)
              {   
                  // if row and column have same value print symbol     
                 if(r==c)      
                    System.out.print((char)(c+ascii)+" ");      
                 else          
                    System.out.print("  ");      
              } 
              System.out.println("");
           } 
           
           // forward
            for(r=1;r<=row;r++)
            {
              // loop for printing star symbol
              for(c=1;c<=row;c++)
              {
                    // if c<= row+1-r print symbol else spaces
                    if(c <= (row+1-r))
                    {
                       if( c == (row+1-r) )
                          System.out.print((char)(c+ascii)+" ");
                       else
                          System.out.print("  ");
                    }
              }
              System.out.println("");
           }
        }
    }
}
Output:

Enter the no of characters in a line = 3
Enter the no of zig zag line = 4

A     
  B   
    C 
    C 
  B 
A 
A     
  B   
    C 
    C 
  B 
A 
A     
  B   
    C 
    C 
  B 
A 
A     
  B   
    C 
    C 
  B 
A

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: