Java Program – Pattern 5

For n = 7

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

Program

public class Pattern {

    public void print(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if ((i + j) % 2 == 0) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
       
    }
}

Given n, the pattern contains n lines. The pattern contains asterisks and spaces alternately. The sum of asterisks and spaces in each line is equal to n. The character at row i and column j is an asterisk if i+j is an even numbner. Else, it is a space.

22 thoughts on “Java Program – Pattern 5

    1. Arka

      @DHANUSH
      class pat{
      public void show(int n){
      int i=0;
      int j=0;

      for(i=1;i=i;j–)
      System.out.print(j+” “);
      }
      }
      }

    2. Shashwat Misra

      class ques
      {
      public void main()
      {
      int i,j;
      for(i=1;i=1;j–)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      }
      }

    3. YAJURVED LODHI

      void print(int n)
      {
      for(int i=1;i=1;j++)
      {
      System.out.print(j);
      }//inner loop
      System.out.println();
      }//outer loop
      }// print

    1. Sahil Gupta

      public class series13
      {
      public static void main()
      {
      for(int i=3;i>=1;i–)
      {
      System.out.println();
      for(int j=1;j=1;k–)
      {
      System.out.print(k);
      }}
      for(int l=2;l<4;l++)
      {
      System.out.println();
      for(int m=1;m=1;n–)
      {
      System.out.print(n);
      }}}}

    2. Soumik Chatterjee

      class pattern
      {
      public static void main(int n)
      {
      int a=1,b=n+1;
      for(int i=1;i<=n;i++)
      {
      for(int j=1;j<=b;j++)
      {
      if(j<=b/2)
      System.out.print(a++);
      else
      System.out.print(–a);
      }
      a=1;
      System.out.println();
      if(i<=n/2)
      b=b-2;
      else
      b=b+2;
      }
      }
      }

  1. YAJURVED LODHI

    void print(int n)
    {
    for(int i=1;i=1;j++)
    {
    System.out.print(j);
    }//inner loop
    System.out.println();
    }//outer loop
    }// print

    Reply
    1. Kevin Andrew

      note:the following code will work only if the entered character is capital.if a special character/small letter is used it will have different outcomes.If a small leter is used,then the first all the capital letter would be printed n then the small leters will get printed(ascii values of small letters come after those of the capital letters) if a special character is used it would have a similar outcome.If u want a code that allows the user to input only capital letters…just check if the entered value is a letter in caps…if its notprint an invaild statement.(if u want the exact code for that,text email me at
      kevinusesspotify@gmail.com

      class pattern12
      {
      public static void main(char ch)
      {
      int n=(int)ch;
      int x=1;
      char f=' ';
      for(int i=65;i<=n;i++)
      {
      for(int j=1;j<=x;j++)
      {
      f= (char)i;
      System.out.print(f);
      }
      System.out.println();
      x=x+1;
      }
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>