Java Program to Print Crown Number Pattern

Program to Print Crown Number Pattern

In the previous article, we have discussed Java Program to Print Ladder Number Pattern

In this article we are going to see how to print crown number program.

When number of rows : 20

0                 0                0
1                 1                1
22             222             22
333         33333         333
4444     4444444     4444
55555 555555555 55555
66666666666666666666
77777777777777777777
88888888888888888888

Now, let’s see the actual program to print it.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach to Print Crown Number Pattern

  • Enter total row and store it in an integer variable row.
  • Take height as h and h=(row -1)/2.
  • Take first for loop to print the row value and number for each row  .
  • Take first inner for loop to print column value i.e., number  according to condition
    if (c == 0 || c  == h  || c == row – 1) and (r == h- 1)and
    if ((c < r || c > h  – r) &&(c < h  + r || c >= row – r)) else print the spaces .
  • Then go on printing the numbers according to loop.

Java Code to Print Crown Number Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,h;
    //creating scanner class object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    h= (row-1)/2;
    for (r = 0; r < h ; r++)
        {
            // printing stars of the crown 
            for (c = 0; c < row; c++)
            {
                // for first row, print '*' 
                // i.e, for top part of crown
                if (r == 0)
                {
                    if (c == 0 || c  == h  || c == row - 1)
                        System.out.print(r);
                    else
                        System.out.print(" ");
                }
                else if (r == h- 1)
                    System.out.print(r);
                else if ((c < r || c > h  - r) &&(c < h  + r || c >= row - r))
                    System.out.print(r);
                else
                    System.out.print(" ");
            }
            // taking to the new line 
            System.out.println();
        }
  }
}
Output:

Enter rows : 20

0                  0                0
1                  1                1
22              222            22
333          33333        333
4444      4444444    4444
555555 55555555 55555
66666666666666666666
77777777777777777777
88888888888888888888

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 Number Pattern Programs: