Go to Top

Monday 1 August 2011

To print a given pattern

Question 19 : Write a program in Java to print the following pattern : 

55555
4444
333
22
1

Java Program : 

public class pattern_6
{
 public static void main()
 {
     int i,j;
     for(i=5;i>=1;i--)
     {
         for(j=1;j<=i;j++)
         {
             System.out.print(i);
            }
            System.out.print("\n");
        }
    }
}

5 comments:

  1. Can you write a program to print this series please?

    *****1
    ****121
    ***12321
    **1234321
    ***12321
    ****121
    *****1
    The stars are actually blanks.

    ReplyDelete
  2. import java.io.*;
    public class M25
    {
    public void main()throws IOException
    {
    int r,c,s,sp,a;
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader br =new BufferedReader(in);
    System.out.println("How many rows do you want?");
    a=Integer.parseInt(br.readLine());
    sp=2*a;
    for(r=1;r<=a;r++)
    {
    for (s=1;s<=sp;s++)
    System.out.print(" ");
    for (c=1;c<=r;c++)
    System.out.print(c+" ");
    sp=sp-2;
    for(c=r-1;c>=1;c--)
    System.out.print(c+" ");
    System.out.println();
    }
    for(r=a-1;r>=1;r--)
    {
    for (s=1;s<=sp+4;s++)
    System.out.print(" ");
    for (c=1;c<=r;c++)
    System.out.print(c+" ");
    sp=sp+2;
    for(c=r-1;c>=1;c--)
    System.out.print(c+" ");
    System.out.println();
    }
    }
    }

    ReplyDelete
  3. write the program for the following output

    arr={444,55555,7777,666,22,11}
    output: arr={5555,7777,444,666,11,22};

    ReplyDelete
  4. write the program for the following output

    1
    3*2
    4*5*6
    10*9*8*7
    this is the program output range was n;

    ReplyDelete
  5. 1234321
    123*321
    12***21
    1*****1

    ReplyDelete

ShareThis