Go to Top

Monday, 1 August 2011

To print a given pattern

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

54321
5432
543
54
5

Java Program : 

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

14 comments:

  1. i need the answer for this pattern

    *
    ***
    *****
    ***
    *

    ReplyDelete
    Replies
    1. for(i=1;i<=5;i=i+2)
      {
      for(j=1;j<=i;j=j+2)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      for(i=3;i>=1;i=i-2)
      {
      for(j=1;j<=i;j=j-2)
      {
      System.out.print("*");
      }
      System.out.println();
      }

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. //output
    //*
    //**
    //***
    //****
    //***
    //**
    //*
    public static void pattern_ch2(){
    for(int row=1; row<=5; row++){
    for(int col=1; col<=row; col++){

    System.out.print("*");
    }
    System.out.println();
    }
    for(int row=4; row>=1; row--){
    for(int col=row; col>=1; col--){
    System.out.print("*");
    }
    System.out.println();
    }
    }

    ReplyDelete
  4. i need that patten
    54321
    5432*
    543**
    54***
    5**** sent me program

    ReplyDelete
  5. can u answer the pattern 5
    54
    543
    5432
    54321

    ReplyDelete
  6. Here you go, Teja

    Java code :


    import java.util.*;
    import java.lang.*;
    import java.io.*;

    class pattern
    {
    public static void main (String[] args)
    {
    int row, col ;

    for(row=5 ; row>=1 ; row--)
    {
    for(col=5 ; col>=row ; col--)
    {
    System.out.print(col) ;
    }

    System.out.print("\n") ;

    }
    }
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. @orange gorgeous

    public class pattern
    {
    public static void main()
    {
    int i,j;
    for(i=1;i<=4;i++)
    {
    for(j=1;j<=4;j++)
    {
    System.out.print("*");
    }
    System.out.print("\n");
    }
    }
    }

    ReplyDelete
  10. Can you help me..
    Pattern program of a+1/3+a+2/5+a+3/7+..... to n

    ReplyDelete
  11. 54321
    4321
    321
    21
    21
    solution for dis???

    ReplyDelete
    Replies
    1. class AB
      {
      public void accept()
      {
      int a=5;int b=1;
      for ( int i=5;i>0;i--)
      {
      for( int j=i;j>=b;j--)
      {
      System.out.print(j);
      }
      System.out.println();
      a--;
      }
      }
      }

      Delete

ShareThis