Friday, September 5, 2014

BlueJ Program And Pattern Printing


In this program we will display the pattern as follows:
1
2 2
3 3 3
4 4 4 4
n n n n n
4 4 4 4
3 3 3
2 2
1

The 'n' represents the number of rows and it should be an odd number.

import java.io.*;
class BPattern
{
 int i,j,x=1,n,m=1;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 void show() throws Exception
 {
     while(true)
     {
         System.out.println("\nHow many rows (It should be odd number):");
         n=Integer.parseInt(br.readLine());
         if(n%2!=0)
         break;
        }
      for(i=1;i< =n;i++)
      {
          for(j=0;j< m;j++)
           {
                System.out.print(x);
             
            }
            System.out.println();
            if(i<=n/2)
            {
                x++;
            m=m+1;
        }
            else
            {
                x--;
            m=m-1;
        }
           
        }
    }
    public static void main(String args[])throws Exception
    {
         BPattern ob=new BPattern();
         ob.show();
        }
    }

Related PostBlueJ Programs On Pattern

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner