Interesting Pattern Program using Array in JAVA

Pattern Program using Array in Java

Question : Write a program to print the following pattern.



 



Program to print the above pattern :-

Program :-

import java.io.*;
import java.util.*;
class Pattern
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int i,j,k=1,n;
        System.out.println("Enter Number of Rows");
        n= in.nextInt(); //input number of rows of pattern
        int ar[][]=new int[n][n]; //2D array for storing number
        for(j=0;j<n;j++)
        {
            if(j%2==0)
            {
                for(i=j;i<n;i++)
                {
                    ar[i][j]=k;
                    k++;
                }
            }
            else
            {
                for(i=n-1;i>=j;i--)
                {
                    ar[i][j]=k;
                    k++;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<=i;j++)
            {
                System.out.print(ar[i][j]+"\t");
            }
            System.out.println();
        }
    }// end of main method
}// end of class


For Proper Understanding Watch the Video :-


All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Smith Number in JAVA

Trimorphic Number in JAVA