About The Site

Search This Blog

Loading...

Monday, September 15, 2014

Printing Pattern on Numbers


Printing the following pattern using BlueJ.


1
1 2 2 1
1 2 4 4 2 1
1 2 4 8 8 4 2 1
1 2 4 8 16 16 8 4 2 1
1 2 4 8 16 32 32 16 8 4 2 1
1 2 4 8 16 32 64 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1


import java.io.*;
class NumberPattern
{
 String str;

 int i,j,n,x=1,y=1;

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 void show() throws Exception
 {
         System.out.println("\nEnter the Number of rows:");
        n=Integer.parseInt(br.readLine());
     
         for(i=1;i<=n;i++)
         {
             x=1;
              for(j=0;j< 2*i-y;j++)
              {
                  if (j >=i)
                  x=x/2;
              System.out.print(x);
              if(j< i)
              x=x*2;
           
            }
Also Read: Computer Teacher in Burdwan


             y=0;
             System.out.println();
            }
        }
       
    public static void main(String args[])throws Exception
    {
         NumberPattern ob=new NumberPattern();
         ob.show();
        }
    }

Printing Any Word in Pattern Format Using BlueJ


Suppose user entered any 'happy' and the following pattern will be displayed.


happyyppah
happ ppah
hap pah
ha ah
h h
ha ah
hap pah
happ ppah
happyyppah

Also Read: Computer Teacher in Burdwan

import java.io.*;
class WordPattern
{
 String str;
 int i,j,k,x=0,len;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 void show() throws Exception
 {
         System.out.println("\nEnter the string:");
         str=br.readLine();
         len=str.length();
         for(i=0;i< len*2-1;i++)
         {
              for(j=0;j< len-x;j++)
              System.out.print(str.charAt(j));
              if(i!=0 && i!=len*2-2)
              System.out.print(" ");
               for(j=len-1-x;j >=0;j--)
              System.out.print(str.charAt(j));
             System.out.println();
            if(i<=len/2+1)
            x++;
            else
            x--;
            }
        }
       
    public static void main(String args[])throws Exception
    {
        WordPattern ob=new WordPattern();
         ob.show();
        }
    }

CHECK HERE FOR PREVIOUS POSTS

Google+ Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner