Program to Print Zig Zag Star Pattern
In this article we will see how to print zig-zag star pattern
Example-1 When no of characters in a line : 3 When no of zig zag lines : 4 * * * * * * * * * * * *
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Approach :
- Print one backward slash first then one forward slash and continue.
- Enter total characters in a row and store it an integer variable say
row. - Enter total no. of zig zag lines and store in an integer variable say
count. - To print Backward slash :
- Take first for loop to print all the rows.
- Take a inner loop to print the column values.
- Then go on printing the star symbols according to the iteration.
- To print forward slash :
- Take first for loop to print all the rows.
- Take inner loop to print the column values.
- Print the star symbol ifÂ
c==(row+1-r).
JAVA Code:
Method-1 : Static Star Character
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;
// 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("* ");
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("* ");
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 * * * * * * * * * * * *
Method-2 : User Input Character
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;
// store the input value in row
row=sc.nextInt();
System.out.print("Enter the no of zig zag line = ");
int count=sc.nextInt();
System.out.print("Enter any character = ");
char ch=sc.next().charAt(0);
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(ch+" ");
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(ch+" ");
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 Enter any character = @ @ @ @ @ @ @ @ @ @ @ @ @
Related Java Star Pattern Programs: