×

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part.

Printing Zig Zag Number Pattern Steps

  • Print one backslash first, followed by one forward slash, then keep going.
  • Put the number of characters in such a row into the integer variable row.
  • Enter count as an integer variable to hold the overall number of zigzags.
  • Take the first for iteration to print all the rows while printing backward slashes.
    • Use an inner loop to output the values for the columns.
    • Afterward, keep printing the numbers in accordance with the iteration.
  • Take the first iteration to print all rows in order to print the forward slash.
    • Print all column values using an inner loop.
    • Afterward, keep printing the numbers in accordance with the iteration.

Let's put the aforementioned actions into a Java program.

// ZigZagStar1.java

import java.util.Scanner;  
public class ZigZagStar1 
{  
    public static void main(String[] args)   
    {  
        // Consider user input regarding no for rowscd desktop   
        System.out.print(" Type in how many characters are on a line: ");  
        Scanner sc = new Scanner(System.in);  
        int row,coloumn,row1;  
        //taking the initial ASCII value 64 
        int ascii=64;  
        // row of storage for the supplied value  
        row1 = sc.nextInt();  
         System.out.print(" Type the number of zigzag lines here: ");  
        int c = sc.nextInt();  
          
        for (int j=1;j<=c;j++)  
        {  
          
            // backward  
           for(row=1; row<=row1; row++)  
           {     
               // number-printing loop
              for(coloumn=1; coloumn<=row1; coloumn++)  
              {     
                  // row and column must have the same value. type a symbol   
                 if(row == coloumn)        
                    System.out.print(row+" ");        
                 else            
                    System.out.print("  ");        
              }   
              System.out.println("");  
           }   
             
           // forward  
            for(row=1;row<=row1;row++)  
            {  
              // number-printing loop  
              for(coloumn=1;coloumn<=row1;coloumn++)  
              {  
                    // if column Equals row 1+1-row, print symbol; otherwise, display spaces.
                    if(coloumn <= (row1+1-row))  
                    {  
                       if( coloumn == (row1+1-row) )  
                          System.out.print(row+" ");  
                       else  
                          System.out.print("  ");  
                    }  
              }  
              System.out.println("");  
           }  
        }  
    }  
}               

Output:          

Zig Zag star and Number Pattern in Java

Zig Zag Star pattern        

 // ZigZagpattern.java

import java.util.Scanner;  
public class ZigZagpattern 
{  
    public static void main(String[] args)   
    {  
        // Consider user input regarding no for rowscd desktop   
        System.out.print(" Type in how many characters are on a line: ");  
        Scanner sc = new Scanner(System.in);  
        int row,coloumn,row1;  
        //taking the initial ASCII value 64 
        int ascii=64;  
        // row of storage for the supplied value  
        row1 = sc.nextInt();  
         System.out.print(" Type the number of zigzag lines here: ");  
        int c = sc.nextInt();  
        System.out.print(" Type any Symbol: ");  
        char s=sc.next().charAt(0);  
          
        for (int j=1;j<=c;j++)  
        {  
          
            // backward  
           for(row=1; row<=row1; row++)  
           {     
               // number-printing loop
              for(coloumn=1; coloumn<=row1; coloumn++)  
              {     
                  // row and column must have the same value. type a symbol   
                 if(row == coloumn)        
                    System.out.print(s+" ");        
                 else            
                    System.out.print("  ");        
              }   
              System.out.println("");  
           }   
             
           // forward  
            for(row=1;row<=row1;row++)  
            {  
              // number-printing loop  
              for(coloumn=1;coloumn<=row1;coloumn++)  
              {  
                    // if column Equals row 1+1-row, print symbol; otherwise, display spaces.
                    if(coloumn <= (row1+1-row))  
                    {  
                       if( coloumn == (row1+1-row) )  
                          System.out.print(s+" ");  
                       else  
                          System.out.print("  ");  
                    }  
              }  
              System.out.println("");  
           }  
        }  
    }  
}                         

Output:

Zig Zag star and Number Pattern in Java

Related Topics

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to Create Immutable Classes in Java

Introduction Java is a programming language that is entirely object-oriented, and everything in it is seen as an object. And the blueprint or template of these objects are classes. Several classes...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.