×

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 vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.