How to Print Pyramid Pattern in Java? Program Example

Pattern based exercises are a good way to learn nested loops in Java. There are many pattern based exercises and one of them is printing Pyramid structure as shown below:


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

You need to write a Java program to print the above pyramid pattern. How many levels the pyramid triangle would have will be decided by the user input. You can print this kind of pattern by using print() and println() method from System.out object. System.out.print() just prints the String or character you passed to it, without adding a new line, useful to print stars in the same line. 

While, System.out.println() print characters followed by a newline character, which is useful to move to the next line. You can also use the Scanner class to get input from the user and draw a pyramid up to that level only. For example in the above diagram, the pyramid has 5 levels.


Analysis

If you look at the problem then you will find that you need to print the star (*) character in the same line as well as a new line to generate a pyramidical pattern. 

You can also see that * are separated by space. In programming, to do a task repeatedly e.g. printing star, you can use a loop. 

This kind of problem, which require printing in row and column usually require two loops, one inside another. Also, known as nested loops. You can use for() loop to create this pattern as shown below:
 public static void drawPyramidPattern() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

There are three loops nested at two level, first is for printing each line and inner loops for printing pattern in each line.




Java Program to Print Pyramid Pattern or Star Pattern

Here is our Java program to draw the pyramid pattern as shown in the problem statement. In this program, we have two examples of printing pyramids, in the first, we have a printed pyramid of star characters, while, in the second example, we have drawn a pyramid of numbers. 

The key here is to use both print() and println() method from PrintStream class, which is also easily accessible as System.out object. We have also used nested for loop to draw the pyramid which you will often use to solve this kind of problem.

How to print Pyramid Pattern in Java with example


Sample code in Java to Print the Pyramid Pattern

import java.util.Scanner;

/**
 * Simple Java Program to draw a pyramid pattern. We have used both
 * System.out.println() and System.out.print() methods to draw stars(*)
 * in pyramid shape.
 * 
 * @author WINDOWS 8
 *
 */
public class PrintPyramidTest {

    public static void main(String args[]) {
        System.out.println("Pyramid pattern of star in Java : ");
        drawPyramidPattern();
        
        System.out.println("Pyramid of numbers in Java : ");
        drawPyramidOfNumbers();
    }

    /**
     * This method draws a pyramid pattern using asterisk character. You can
     * replace the asterisk with any other character to draw a pyramid of that.
     */
    public static void drawPyramidPattern() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    
    
    /**
     * This method draws a pyramid of numbers. 
     */
    public static void drawPyramidOfNumbers() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output :
Pyramid pattern of star in Java : 
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
Pyramid of numbers in Java : 
     0 
    0 1 
   0 1 2 
  0 1 2 3 
 0 1 2 3 4 

And, if you want to do more coding practice, here are many more pyramid and star pattern exercises which you can try to solve, this will improve your coding skills significantly:

Pyramid and star pattern exercises
image_credit - geeksforgeeks.org



That's all about how to print Pyramid using Java pattern. You can find many pattern printing exercises in Java or C++ programming books. You can further refine this program to print any other character instead of * or you can ask the user to enter the number of rows. You can even modify this program to print a pyramid of numbers.


Thanks for reading this article. If you like then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note. 

If you have any questions or doubt then please let us know and I'll try to find an answer for you. 

Btw, what is your favorite coding exercise? prime number, palindrome or this one?

480 comments:

  1. Image

    Hi,
    We can print triangle using two loops only. :)

    for (int i=0;i<=5;i++) {

    for (int j=0;j<i;j++) {
    System.out.print("* ");
    }
    System.out.println();
    }

    ReplyDelete
    Replies
    1. Image

      Yes, you can and code is fine as well but looks couple of more adjustments
      - it prints empty line in first iteration, to avoid that check for j <=i in second loop
      - since you are starting from zero in first loop condition should be i<5 , no need of equal otherwise it will print 6 lines.

      Delete
    2. Image

      ... just one cycle is enough:

      StringBuilder sb = new StringBuilder();
      String sep = "";
      for (int i=0; i< 5; i++) {
      System.out.println( sb.append(sep).append("*") );
      sep = " ";
      }

      Delete
    3. Image

      sir pls print this for me
      **0**

      *0*0*

      0*0*0

      *0*0*

      **0**

      Delete
    4. Image

      1 0 0 0 0 0 0 1
      3 3 0 0 0 0 3 3
      5 5 5 0 0 5 5 5
      7 7 7 7 7 7 7 7
      5 5 5 0 0 5 5 5
      3 3 0 0 0 0 3 3
      1 0 0 0 0 0 0 1
      I want pattern like this...Pls anyone can do this...???

      Delete
    5. Image

      ineed a pattern to print emoticons any help?

      Delete
  2. Image

    public class simple3 {

    public static void main(String[] args)
    {



    for(int i =0;i<=5;i++)
    {
    for(int j=0;j<i;j++)
    {
    System.out.print(" "+i);


    }
    System.out.println(" ");
    }






    }
    }







    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
    6 6 6 6 6 6

    ReplyDelete
    Replies
    1. Image

      for(int i=1; i<=5; i++)
      {
      int countr = 1;
      while(i>=countr)
      {
      System.out.print(i + " ");
      countr++;
      }

      System.out.println("");
      }

      Delete
    2. Image

      how to write program to display
      ####
      ###
      ##
      #

      Delete
    3. Image

      int i;
      for( i = 0; i <4; i++)
      {
      for (int j = 0;j<4-i;j++)
      {
      System.out.print("#");
      }
      for(int k = 0; k <=i; k++)
      {
      System.out.print("");
      }
      System.out.println();
      }

      Delete
    4. Image

      for (int i = 4; i >= 1; i--) {

      for (int j = 1; j <= i; j++) {

      System.out.print(" # ");
      }

      System.out.println();
      }

      Delete
    5. Image

      what is the code to get output as
      * *
      ** **
      *** ***
      **** ****
      *********

      Delete
  3. Image

    Can anyone help me.. How to print two star pyramid triangular connecting each other....

    ReplyDelete
    Replies
    1. Image

      for (int i = 0; i < 5; i++)
      {
      for (int j = 0; j < 5 - i; j++)
      {
      System.out.print(" ");

      }
      for (int k = 0; k <= i; k++)
      {
      System.out.print("* ");

      }
      for (int l = 0; l < 5 - i; l++)
      {
      System.out.print("^ ");

      }
      System.out.println();
      }

      Delete
  4. Image

    Can anyone say how to print two star pyramid with spaces

    ReplyDelete
    Replies
    1. Image

      class Pattern1
      {
      void main(int n)
      {
      for(int i=n;i>=1;i--)
      {
      for(int j=1;j<=n-i;j++)
      {
      System.out.print(" ");
      }
      for(int j=1;j<=i;j++)
      {
      System.out.print("*");
      }
      for(int j=i-1;j>=1;j--)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      for(int i=2;i<=n;i++)
      {
      for(int j=1;j<=n-i;j++)
      {
      System.out.print(" ");
      }
      for(int j=1;j<=i;j++)
      {
      System.out.print("*");
      }
      for(int j=i-1;j>=1;j--)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

      Delete
  5. Image

    Display the code for this:

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

    ReplyDelete
    Replies
    1. Image

      public class abc
      {
      public static void main(String args[])
      {
      for(int i=2;i<=4;i+=2)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      for(int k=1;k<=7;k++)
      {
      System.out.print("*");
      }
      System.out.println();
      }

      Delete
    2. Image

      It was very simple

      Delete
    3. Image

      public class Pattern {

      public static void main(String[] args) {
      int k = 2;
      for (int i = 2; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
      System.out.print("*");
      }
      k += i;
      System.out.println();
      }
      }

      }

      Delete
    4. Image

      public class Pattern {

      public static void main(String[] args) {
      int k = 2;
      for (int i = 2; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
      System.out.print("*");
      }
      k += i;
      System.out.println();
      }
      }

      }

      Delete
    5. Image

      public class Pattern8 {

      public static void main(String[] args) {
      int k = 2;
      for (int i = 2; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
      System.out.print("*");
      }
      k += i;
      System.out.println();
      }
      }

      }

      Delete
  6. Image

    Display the code for this:

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

    ReplyDelete
    Replies
    1. Image

      In third line if we have to print six star then code is:
      for(int i=0;i<4;i++)
      {
      for(int j=0;j<i*2;j++)
      {
      print("*");
      }
      println();
      }

      Delete
  7. Image

    *
    **
    ***
    ****
    *****
    print this with only two loops

    ReplyDelete
    Replies
    1. Image

      for (int i = 0; i < n; i++)
      {
      for (int k = 0; k <= i; k++)
      {
      System.out.print("* ");
      }

      System.out.println();
      }

      Delete
    2. Image

      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      sop("*");
      }
      sopln();
      }

      Delete
  8. Image

    No I'm as asking for two pyramids not for triangles

    ReplyDelete
  9. Image

    I asked for double pyramids not triangle

    ReplyDelete
  10. Image

    1 2 3 4
    8 7 6 5
    9 10 11 12
    16 15 14 13

    Help me to solve this problem

    ReplyDelete
    Replies
    1. Image

      int count = 1;
      for(int i=1;i<=4;i++)
      {
      for(int j=1;j<=4;j++)
      {
      if(i%2!=0)
      {
      System.out.print(count++);
      }
      else
      {
      System.out.print(count--);
      }
      }
      if(i%2!=0)
      {
      count = count+3;
      }
      else
      {
      count = count+5;
      }
      System.out.println();
      }

      Delete
    2. Image

      int m = 1;
      for(int i=1; i<=5; i++)
      {
      if(i % 2 != 0)
      {
      for(int j = m;j <= m+4;j++)
      {
      System.out.print( j+ " ");
      }
      }
      else
      {
      for(int j = m+4;j >= m;j--)
      {
      System.out.print( j+ " ");
      }
      }
      System.out.println("");
      m= m+5;
      }

      Delete
    3. Image

      public static void main(int n)
      {int c=1,j=0,k=1,l=0;
      for(int i=1;i<=n;i++)
      {

      if(i%2!=0)
      {for( j=k;j<=(n*i);j++)
      System.out.print(j+" ");
      }
      else
      {
      for( k=j, l=(n*i);k<=(n*i);k++,l--)
      System.out.print(l+" ");

      }
      System.out.println();

      }
      }

      Delete
    4. Image

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      int l = 1;
      int n = 4;
      for (int i = 1; i <=n; i++) {

      if(i%2!=0){
      for (int j = 1; j <=n; j++) {
      System.out.print(l++);
      }
      }
      else
      {
      for (int j = 1; j <=n; j++) {
      System.out.print(l+4-j);
      }
      l=l+4;
      }

      System.out.println("");
      }
      }

      Delete
    5. Image

      package com.test;

      public class Test {

      public static void main(String[] args) {
      int count=0; // TODO Auto-generated method stub
      int k;
      for(int i=1;i<16;i++)
      {

      if(count==4)
      {
      count=1;System.out.println("");
      int count1=0;
      for(k=i+3;k<=16;k--)
      {
      i++;
      System.out.print(k+" ");
      count1++;
      if(count1==4)
      {
      System.out.println("");
      break;
      }

      }
      }
      else{count++;}
      if(i>16)
      break;
      System.out.print(i+" ");

      }


      }

      }

      Delete
  11. Image

    How to print these patterns:
    (i)
    *
    *
    *
    *
    *
    (ii)
    * *
    * *
    **
    * *
    * *

    ReplyDelete
    Replies
    1. Image

      for(i=0;i<5;i++)
      {
      for(j=0;j<1;j++)
      System.out.print(" * ");
      System.out.println();
      }

      Delete
    2. Image

      (ii)
      for(i=0;i<5;i++)
      {
      for(j=0;j<2;j++)
      System.out.print(" * ");
      System.out.println();
      }

      Delete
    3. Image

      (I)
      class Pattern
      {

      public static void main (String[] args)
      {
      for(int i =0;i<=5;i++)
      {
      System.out.print(" "+i);

      System.out.println(" ");
      }
      }

      }

      Delete
    4. Image

      public static void main(String[] args) {
      for(int i=1;i<=5;i++)
      {
      for( int j=1;j<=2;j++){
      if(i%3!=0){
      System.out.print("*");
      System.out.print(" ");
      }
      else
      {
      System.out.print("*");


      }}
      System.out.println();
      }}

      Delete
  12. Image

    (i)
    for(i=0;<5;i++)
    {
    for(j=0;j<1;j++)
    System.out.print(" * ");
    System.out.println();

    }

    ReplyDelete
    Replies
    1. Image

      compilaction error..!!
      bcz illegal start of type

      Delete
    2. Image

      illegal start of type...!!
      bcz for(i=0;<5;i++) wrong
      for(i=0;i<5;i++);

      Delete
  13. Image

    Any one please help me to send below Java pattern program on my email id
    [email protected]

    *
    ++
    ***
    ++++
    *****

    ReplyDelete
    Replies
    1. Image

      class test
      {
      public static void main(String args[])
      {
      java.util.Scanner sc=new java.util.Scanner(System.in);
      int row=sc.nextInt();
      for(int i=0;i<=row;i++)
      {
      for(int j=0;j<=i;j++)
      {
      if((i%2)!=0)
      System.out.print("*");
      else
      System.out.print("+");
      }
      System.out.println();
      }
      }
      }

      Delete
    2. Image

      you r rocking bro

      Delete
  14. Image

    public class Pyramid {

    public static void main(String []args)
    {
    for(int i=0;i<10;i++)

    {if (i%2!=0)
    for(int j=0;j<i;j++)
    {

    System.out.print("* ");

    }
    else
    for(int k=0;k<i;k++)
    {
    System.out.print("+ ");
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  15. Image
    Replies
    1. Image

      class Test4
      {
      public static void main(String[] args)
      {
      int a=65;
      for (int i=1;i<=5;i++)
      {
      for (int j=5;j>=i;j--)
      {
      char ch=(char)a;
      System.out.print(ch);

      }
      a++;
      System.out.println();
      }

      }
      }

      Delete
  16. Image

    please solve this below two problems
    (i)12345
    1234
    123
    12
    1
    (ii)5555
    4444
    3333
    2222
    1111

    ReplyDelete
    Replies
    1. Image

      For your first question following is the program:
      public class PyramidNumber {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      for (int i=5;i>=0;i--){
      for (int j=1;j<=i;j++){
      System.out.print(j);
      }//End of j loop
      System.out.println("");
      }//End of i loop

      }

      }

      For your second question , here you don't have to user nested loop. only one loop is enough , i have commented the first loop in the program:
      public class PyramidSamerowNumbers {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      //for (int i=5;i>0;i--){
      for (int j=5;j>=1;j--){
      System.out.print(j);
      System.out.print(j);
      System.out.print(j);
      System.out.print(j);
      System.out.println("");
      }
      System.out.println("");
      //}

      }

      }

      Delete
    2. Image

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      for (int i = 0; i < 5; i++) {
      for (int j = 1; j <=5-i ; j++) {
      System.out.print(j);
      }
      System.out.println("");
      }
      }

      Delete
    3. Image

      public static void main(String[] args) {

      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(""+j);

      }
      System.out.println("");

      }
      }
      }

      Delete
    4. Image

      public static void main(String[] args) {

      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(""+j);

      }
      System.out.println("");

      }
      }
      }

      Delete
  17. Image

    how to print this
    *****
    * *
    * *
    * *
    *****

    ReplyDelete
    Replies
    1. Image

      class Star2
      {
      public static void main(String[] args)
      {
      int n=5;
      for(int i=0;i<n;i++)
      {
      int c=0;
      for(int j=0;j<n;j++)
      {
      if(i==0||i==n-1)
      {
      System.out.print("*");
      }
      else
      {
      if(c<2)
      {
      System.out.print("* ");
      c++;
      }


      }
      }
      System.out.println();
      }
      }
      }

      Delete
  18. Image

    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

    how to create

    ReplyDelete
    Replies
    1. Image

      for(int i=1; i<=5; i++)
      {
      int s = 4,cnt=1;
      int r=i;
      int sum = i;
      while(cnt= 0 )
      {
      System.out.print( sum + " ");
      sum = sum+s;
      s--;
      }
      }
      System.out.println("");
      }

      Delete
    2. Image

      for(int i=1; i<=5; i++)
      {
      int s = 4,cnt=1;
      int r=i,sum = i;
      while(cnt= 0 )
      { System.out.print( sum + " ");
      sum = sum+s;
      s--;
      }
      }
      System.out.println("");
      }

      Delete
    3. Image

      while(cnt=0)----- may be it's error.....!!!....???

      Delete
    4. Image

      for (int i = 1; i < 6; i++) {
      if(i==1)
      {
      System.out.print(i);
      //System.out.println();
      }
      else
      {
      int sum=i;
      int k=4;
      System.out.print(i);
      for (int j = 1; j < i; j++) {


      sum=sum+k;
      System.out.print(sum);
      k--;

      }
      }
      System.out.println();

      Delete
  19. Image

    1
    12
    123
    1234
    12345
    1234
    123
    12
    1

    plz can any one give code for this program

    ReplyDelete
    Replies
    1. Image

      class Pattern
      {
      public static void main(String[] args)
      {
      int ck=0,c=2;
      while(c>0)
      {
      if(ck==0)
      {
      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      ck++;
      }
      else
      {
      for(int i=1,r=5-1;i<=5-1;i++,r--)
      {
      for(int j=1;j<=r;j++)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      }
      c--;
      }
      }
      }

      Delete
    2. Image

      package manoj2;

      public class Patern {
      public static void main(String[] args)

      {
      int m=5;
      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(m);

      }
      m--;
      System.out.println("");
      }

      int n=2;
      for(int i=1;i<=4;i++)
      {

      for(int j=4;j>=i;j--)
      {
      System.out.print(n);
      }
      n++;
      System.out.println("");
      }
      }
      }

      Output:
      5
      44
      333
      2222
      11111
      2222
      333
      44
      5

      Delete
    3. Image

      public static void main(String []args){
      for (int i = 1; i <= 6; i++)
      {
      for(int j=1;j<i;j++)
      {
      System.out.print(j);

      }
      System.out.println();
      }
      for (int i = 1; i <= 6; i++)
      {
      for(int k=1;k<6-i;k++)
      {
      System.out.print(k);

      }
      System.out.println();
      }
      }

      Delete
  20. Image

    * *
    * * * *
    * * * * * *
    * * * * * * * *
    how to print this pattern in java ,[email protected]

    ReplyDelete
    Replies
    1. Image

      public class Pattern4{

      public static void main(String args[]){

      int i,j;

      for(i=0;i<5;i++){
      for(j=0;j<i*2;j++){

      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

      Delete
  21. Image

    Hi how can you display this output?

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

    when the input is 5?

    ReplyDelete
    Replies
    1. Image

      import java.util.*;

      public class Pattern5{

      static Scanner s=new Scanner(System.in);
      public static void main(String args[]){

      int i,j,n;

      System.out.print("Input Number : ");
      n=s.nextInt();

      for(i=0;i<=n;i++){
      for(j=0;j<i;j++){

      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

      Delete
  22. Image

    Please help me print this pattern

    5
    4 4
    3 3 3
    2 2 2 2
    1 1 1 1 1
    2 2 2 2
    3 3 3
    4 4
    5

    ReplyDelete
    Replies
    1. Image

      package manoj2;

      public class Patern {
      public static void main(String[] args)

      {
      int m=5;
      for(int i=1;i<=5;i++)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(m);

      }
      m--;
      System.out.println("");
      }

      int n=2;
      for(int i=1;i<=4;i++)
      {

      for(int j=4;j>=i;j--)
      {
      System.out.print(n);
      }
      n++;
      System.out.println("");
      }
      }
      }

      Delete
    2. Image

      public static void main(String[] args) {
      for (int i=5;i>=1;i--){
      for (int j=1;j<=6-i;j++){
      System.out.print(i);
      }System.out.println();
      }
      for (int i=1;i<5;i++){
      for (int j=1;j<=5-i;j++){
      System.out.print(1+i);}
      System.out.println();
      }}

      Delete
  23. Image

    How do you print following pyramid pattern of stars in Java:

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

    ReplyDelete
    Replies
    1. Image


      public class Pattern10 {
      public static void main(String[] args) {
      int k=8;
      for (int i = 6; i <=7 ;i++) {
      for (int j = 1; j <=i ; j++) {
      System.out.print("*");

      }
      System.out.println();
      }
      while(k<=12)
      {
      for (int i = 1; i <=3; i++) {
      for (int j = 1; j <=k; j++) {
      System.out.print("*");
      }
      k--;
      System.out.println();
      }
      k=k+5;
      }
      }
      }

      Delete
  24. Image

    Hello Javin, I am big fan of your both Javarevisited. I have been reading from years and it inspired me to create my own blog. I would like to share my latest article with you about printing patterns of numbers in Java, please have a look when you get some time.

    ReplyDelete
  25. Image

    Can you write Java code for the given program. Ex: [email protected] replace all the characters with '*'except'j'and '@gmail.com' output: j*****[email protected]

    ReplyDelete
  26. Image

    Write a Java prg given below. Input: [email protected]. output: j******[email protected]

    ReplyDelete
  27. Image

    Can you write java program for this pattern.

    ReplyDelete
  28. Image

    can anybody write a java program for this pattern:
    https://1.bp.blogspot.com/-XwSWmFZ9o6k/V05e57pzA5I/AAAAAAAAHRc/jwbfL9kLGIoCPpeIlY6BDSZtorlE-kwngCLcB/s640/tempFileForShare_2016-06-01-09-19-47.jpg

    ReplyDelete
  29. Image

    Hi how can you display this output?
    abcde abcde
    abcd bcde
    abc cde
    ab de
    a e

    ReplyDelete
    Replies
    1. Image
    2. Image

      import java.io.*;

      class patt1{

      public static void main(String args[]){

      char a=(char)97,b,c;

      int i,j,n=5;


      for(i=0;ii;j--){
      System.out.print(b);
      b++;
      }

      for(int k=0;ki;j--){
      System.out.print(c);
      c++;
      }
      System.out.print("\n");

      }

      }

      }

      Delete
    3. Image
  30. Image

    please help me with this....

    *
    *A*
    *A*A*
    *A*A*A*

    ReplyDelete
    Replies
    1. Image

      class pat
      {
      public static void main()
      {
      int i,j;
      for(i=1;i<=5;i++)
      {
      for(j=1;j<=i;j++)
      {
      if(j%2==0)
      System.out.print("A");
      else
      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

      Delete
  31. Image

    can u please write the code for below pattern
    A B C D E A B C D E
    A B C D A B C D
    A B C A B C
    A B A B
    A A
    A B A B
    A B C A B C
    A B C D A B C D
    A B C D E A B C D E

    ReplyDelete
    Replies
    1. Image

      999999999999999999
      88888888 88888888
      7777777 7777777
      55555 55555
      4444 4444
      333 333
      22 22
      1 1

      public static void main(String[] args) {
      int number = 9;
      for(int i=0;i<9;i++){
      for(int j=0;j<18;j++){
      if(number==6)
      continue;
      System.out.print(number);
      }
      if(number != 6)
      System.out.println();
      number--;
      }
      }

      Delete
    2. Image

      public static void main(String[] args) {
      // TODO code application logic here
      int i, j,l;
      for(i=5;i>=1;i--)
      {
      for(j=1;j<=i;j++)
      System.out.print(j);
      for(j=1;j<=i;j++)
      System.out.print(j);
      System.out.println();}
      for(i=2;i<=5;i++)

      {
      for(j=1;j<=i;j++)
      System.out.print(j);
      for(j=1;j<=i;j++)
      System.out.print(j);
      System.out.println();}
      }
      }

      Delete
  32. Image

    I want to write a program in Java that was published the following output :

    1
    2 3 2
    3 4 5 4 3
    4 5 6 7 6 5 4
    5 6 7 8 9 8 7 6 5

    ReplyDelete
  33. Image

    This comment has been removed by the author.

    ReplyDelete
  34. Image

    how to code this
    *
    **
    ***
    ****
    *****
    ******
    *
    *
    ***

    ReplyDelete
  35. Image
  36. Image

    How to print the below pattern
    *!
    *! *-
    *! *- *!
    *! *- *! *-
    *! *- *! *- *!

    ReplyDelete
  37. Image

    How to print this...
    1
    2 1
    3 2 1
    4 3 2 1
    5 4 3 2 1

    ReplyDelete
  38. Image

    Guys i have found easy way to print pattern
    public class Try {

    public static void main(String[] args) {

    int count =6;

    for(int i=5;i>0;i--)
    {
    count--;

    for(int j=count;j>0;j--)
    {
    System.out.print("*");

    }

    System.out.println();
    }



    }
    }
    ===================output==========================

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

    ReplyDelete
  39. Image

    how to print
    *
    * *
    * * *
    * * * *
    * * *
    * *
    *

    ReplyDelete
    Replies
    1. Image

      public class Star3 {

      public static void main(String[] args) {

      int n = 10;

      for (int i = 1; i <= n; i++) {


      for (int j = 1; j <= i; j++) {
      System.out.print("*");
      }
      System.out.println();

      }


      for (int i = n; i >= 1; i--) {


      for (int j = 1; j <= i; j++) {
      System.out.print("*");
      }
      System.out.println();
      }
      }
      }

      Delete
    2. Image

      how to reverse the logic i mean to print in decreasing order from 10stars to 1 and from 1start to 10 could anyone help me out in doin this

      Delete
  40. Image

    write a program in java that prints the following pyramid:

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

    ReplyDelete
    Replies
    1. Image

      /* program 11: Full Number Pattern 11 Pyramid
      */
      //imports
      import java.util.Scanner;


      class FullNumberPatternPyramid11
      {
      public static void main(String[] args)
      {
      //declarations
      int input,RefNum;


      System.out.println("enter the size of the pyramid");
      input=new Scanner(System.in).nextInt();
      if(input>0)
      {
      for (int rows=1;rows<=input ;rows++ )
      {
      RefNum=rows;
      for(int spaces=1;spaces<=input-rows;spaces++)
      {
      System.out.print(" ");
      }//for
      for (int col=1;col<=(2*rows)-1 ;col++ )
      {
      if(col<=rows)
      {
      System.out.print(col);
      System.out.print(" ");
      }//if
      else
      {
      RefNum--;
      System.out.print(RefNum);
      System.out.print(" ");
      }//else
      }//for
      System.out.println();
      }//for
      }//if
      else
      {
      System.out.println("enter positive integers only");
      }//else
      }//main
      }//class

      Delete
  41. Image

    Can somebody write a program in java that prints the following pyramid:

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

    ReplyDelete
    Replies
    1. Image

      int n = 10;

      // Loop through the lines from 1 to n
      for (int i = 1; i <= n; i++) {
      // printing spaces, 4 at a time from j=0 to j= n-i
      for (int j = 1; j <= (n - i); j++)
      {
      System.out.print(" ");
      }

      // Printing number increamentally from 0 to i-1
      for (int j = 0; j < i; j++) {
      System.out.printf("%4d", (int) Math.pow(2, j));
      }

      // Printing number decreamentally from i-2 to 0
      for (int j = i - 2; j >= 0; j--) {
      System.out.printf("%4d", (int) Math.pow(2, j));
      }
      System.out.println();

      }

      }

      Delete
  42. Image

    public class Star3 {

    public static void main(String[] args) {

    int n = 10;

    for (int i = 1; i <= n; i++) {


    for (int j = 1; j <= i; j++) {
    System.out.print("*");
    }
    System.out.println();

    }


    for (int i = n; i >= 1; i--) {


    for (int j = 1; j <= i; j++) {
    System.out.print("*");
    }
    System.out.println();
    }





    for (int i = 1; i <= n; i++) {


    for (int j = 1; j <= n; j++) {
    if (i + j > n) {
    System.out.print("*");
    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }



    for (int i = n; i >= 1; i--) {

    for (int j = 1; j <= n; j++) {
    if (i + j > n) {
    System.out.print("*");

    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }




    }
    }

    ReplyDelete
  43. Image

    public class Star3 {

    public static void main(String[] args) {

    int n = 10;

    for (int i = 1; i <= n; i++) {


    for (int j = 1; j <= i; j++) {
    System.out.print("*");
    }
    System.out.println();

    }


    for (int i = n; i >= 1; i--) {


    for (int j = 1; j <= i; j++) {
    System.out.print("*");
    }
    System.out.println();
    }





    for (int i = 1; i <= n; i++) {


    for (int j = 1; j <= n; j++) {
    if (i + j > n) {
    System.out.print("*");
    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }



    for (int i = n; i >= 1; i--) {

    for (int j = 1; j <= n; j++) {
    if (i + j > n) {
    System.out.print("*");

    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }




    }
    }

    ReplyDelete
  44. Image

    00
    1001
    010010
    10100101
    0101001010
    101010010101
    101010010101
    0101001010
    10100101
    010010
    1001
    00

    ReplyDelete
  45. Image

    print this please using loops

    *
    *
    * *
    * *
    * * *

    ReplyDelete
    Replies
    1. Image

      public class Star3 {

      public static void main(String[] args) {

      int n = 10;

      for (int i = 1; i <= n; i++) {


      for (int j = 1; j <= i; j++) {
      System.out.print("*");
      }
      System.out.println();

      for (int j = 1; j <= i; j++) {
      System.out.print("*");
      }
      System.out.println();

      }

      }

      }

      Delete
  46. Image

    *
    **
    ***
    **
    * give me the program pls
    *

    ReplyDelete
  47. Image

    How to print
    A
    AB
    ABC
    ABCD
    ABCDE

    ReplyDelete
  48. Image

    please help this pattern
    *
    *-*
    *-*-*
    *-*-*-*

    ReplyDelete
  49. Image

    1
    01
    101
    0101

    class pattern
    {
    public static void main(String args[])
    {
    for(int i=1;i<=4;i++)
    {
    for(int j=1;j<=i;j++)
    {
    if((i+j)%2==0)
    {
    System.out.print("1");
    }
    else
    {
    System.out.print("0");
    }}
    System.out.println();
    }
    }
    }

    ReplyDelete
  50. Image

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

    please given the Logic........?????

    ReplyDelete
  51. Image
  52. Image
    Replies
    1. Image

      class patt5
      {
      public static void main(String[]args)
      {
      int i,j;
      for(i=0;i<6;i++)
      {
      for(j=0;j<i;j++)
      {
      System.out.print(i+j);
      }
      System.out.println();
      }
      }

      }

      Delete
    2. Image

      in first line, it will print 0

      Delete
  53. Image

    1
    22 22
    333 333 333
    4444 4444 4444 4444

    ReplyDelete
    Replies
    1. Image

      /*
      Print this output
      1
      22 22
      333 333 333
      4444 4444 4444 4444
      */
      public class pattern {
      public static void main(String[] args) {
      int i=0;
      int a=8; // its ur number to be patterned
      System.out.println("========");
      for (i=1; i<=a;i++){
      for(int j=1;j<=i;j++){
      System.out.print(i);
      if(i>1){
      for(int k=2; k<=i;k++){
      System.out.print(i);
      }
      System.out.print(" ");
      }
      }
      System.out.println();
      }
      }

      }

      Delete
  54. Image

    i want a pattern of boat ....can anyone help me...pattern should like..

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

    ReplyDelete
  55. Image
    Replies
    1. Image

      int x=1;
      for(int i=0;i<=4;i++)
      {
      for(int j=0; j<=i;j++)
      {
      System.out.print(x);
      x++;
      }
      System.out.println();
      }

      Delete
  56. Image
  57. Image

    Hi how can you display this output?

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

    when the input is 5 and using only while loop ?
    Thanks.

    ReplyDelete
  58. Image

    How to print below java pattern?
    1
    72
    863
    10954


    ReplyDelete
  59. Image

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

    ReplyDelete
  60. Image
  61. Image

    HOW To Print??

    1******
    12*****
    123****
    1234***
    12345**
    123456*
    1234567

    ReplyDelete
    Replies
    1. Image

      int i,j,n=1,k;
      for(i=1;i<=7;i++){
      for(j=1;j<=i;j++){
      System.out.print(""+j);
      }
      for(k=n;k<=6;k++){
      System.out.print("*");

      }
      n++;
      System.out.println(" ");
      }

      Delete
  62. Image
  63. Image

    Hi how to print *
    *****
    ***
    *
    Mean to say asterik symbol with asteriks

    ReplyDelete
    Replies
    1. Image

      How to print
      * *
      * *
      *
      * *
      * *

      Pls help

      Delete
    2. Image

      public static void main(String[] args) {
      int i, j;
      for(i=3;i>1;i--){
      for(j=1;j<=2;j++){
      System.out.print("*");
      }
      System.out.println(" ");
      }
      for(i=1;i>=1;i--){
      System.out.println("*");
      }

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

      Delete
    3. Image
  64. Image

    can u pls tell me how to print
    *****
    * *
    * *
    * *
    *****

    ===A Hollow square??

    ReplyDelete
  65. Image
    Replies
    1. Image

      import java.io.*;
      class Pat3
      {
      public static void main(String s[])
      {
      for (int i = 0; i <= 5; i++)
      {
      for (int j = 0; j < i; j++)
      {
      System.out.print("*");
      }
      System.out.println(" ");
      }
      }
      }

      Delete
  66. Image

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

    ReplyDelete
  67. Image

    public class Aryan
    {
    public static void main(String[] args) {
    for (int i = 11; i >=1; i--) {
    for (int j = 1; j <=i; j++) {
    System.out.print("*");
    }
    System.out.println();
    }
    for (int i = 1; i <=11; i++) {
    for (int j = 1; j <=i; j++) {
    System.out.print("*");
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  68. Image

    sir how to print

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

    ReplyDelete
  69. Image

    what about this?
    ABCDEF
    BCDEF
    CDEF
    DEF
    EF
    F

    ReplyDelete
  70. Image

    Thanks for this site, an another way to print the star pattern in triangular shape is:
    public static void main(String[] args) {
    for(int i=0;i<=4;i++)
    {
    for(int j=0;j<=4;j++)
    {
    if(j<4-i)
    {
    System.out.print(" ");
    }
    else
    {
    System.out.print("* ");
    }

    }
    System.out.println();

    }
    }

    ReplyDelete
  71. Image

    how can print this pattern:
    1
    21
    321
    4321
    54321

    ReplyDelete
  72. Image

    How to print this pattern

    ---*
    --**
    -***
    ****

    in Java (excluding that dashes)

    ReplyDelete
  73. Image

    hi...how to print the following output
    ####
    ###
    ##
    #

    ReplyDelete
  74. Image

    1
    12
    123
    1234
    123
    12
    1
    please solve this?

    ReplyDelete
  75. Image

    2
    4 4
    6 6 6
    8 8 8 8
    hey.... How to print this output??

    ReplyDelete
    Replies
    1. Image

      class PritStarTraingle{
      public static void main(String args[]) {
      int i,j,k=2;
      for(i=0;i<4;i++)
      {
      for(j=0;j<=i;j++){
      System.out.print(k);
      }
      k=k+2;
      System.out.println();
      }


      }
      }

      Delete
  76. Image

    how to print this pattern pls ... i need it badly please
    * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *
    using for and while loop with bufferedreader?

    ReplyDelete
  77. Image

    1 1
    1 2 1
    1 2 2 1
    1 2 2 2 1
    1 2 2 2 2 1
    How to print this output??

    ReplyDelete
  78. Image

    print 1
    1 2 1
    1 2 4 2 1
    1 2 4 8 4 2 1
    1 2 4 8 16 8 4 2 1

    ReplyDelete
  79. Image

    1
    1 2 1
    1 2 4 2 1
    1 2 4 8 4 2 1

    ReplyDelete
  80. Image

    1
    12
    123
    1234
    12345
    How to print this output?

    ReplyDelete
  81. Image

    1
    12
    123
    1234
    12345
    How to print this output??

    ReplyDelete
    Replies
    1. Image

      import java.io.*;
      class Pat4
      {
      public static void main(String s[])
      {
      for (int i = 0; i <= 5; i++)
      {
      for (int j = 0; j < i; j++)
      {
      System.out.print(j+1);
      }
      System.out.println(" ");
      }
      }
      }

      Delete
    2. Image

      class PritStarTraingle{
      public static void main(String args[]) {
      int i,j;
      for(i=1;i<6;i++)
      {
      for(j=1;j<=i;j++){
      System.out.print(j);
      }

      System.out.println();
      }


      }
      }

      Delete
  82. Image

    public static void main(String[] args) {

    int i,j,n=1,k;
    for(i=1;i<=7;i++){
    for(j=1;j<=i;j++){
    System.out.print(""+i*2);
    }
    for(k=n;k<=6;k++){
    System.out.print("*");

    }
    n++;
    System.out.println(" ");
    }
    }

    ReplyDelete
  83. Image

    can anyone plz help me out with this pattern
    7
    14 15
    28 29 30 31
    56 57 58 59 60 61 62 63

    ReplyDelete
  84. Image

    hi does anyone know ho to print a triangle with a shape chosen by the user ?? (with textIO or scanner) thanks alot

    ReplyDelete
  85. Image
  86. Image

    Help me in printing this:
    1
    32
    456
    10987
    1112131415

    ReplyDelete
  87. Image
  88. Image

    how to printing this? plz help

    1
    22
    333
    4444
    55555

    ReplyDelete
    Replies
    1. Image

      class PritStarTraingle{
      public static void main(String args[]) {
      int i,j;
      for(i=1;i<6;i++)
      {
      for(j=1;j<=i;j++){
      System.out.print(i);
      }

      System.out.println();
      }


      }
      }

      Delete
  89. Image

    This program to print a triangle..

    class StarTriangle
    {
    public static void main(String[] args)
    {
    int i,j,k;
    for(i=1; i<=5; i++)
    {
    for(j=4; j>=i; j--)
    {
    System.out.print(" ");
    }
    for(k=1; k<=(2*i-1); k++)
    {
    System.out.print("*");
    }
    System.out.println("");
    }
    }
    }

    9Apps Apk

    ReplyDelete
  90. Image

    How to print that pattern(excluding '-'):

    --------*
    ------*-*
    ----*-*-*
    --*-*-*-*
    *-*-*-*-*

    Thanks in advance.

    ReplyDelete
    Replies
    1. Image

      public class PrintStar {

      public static void main(String a[])
      {
      int i, j, k;
      for(i=5;i>=1;i--)
      {
      for(j=1;j<=i;j++)
      {
      System.out.print(" ");
      }
      for(k=5;k>=i;k--)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      }

      }

      Delete
  91. Image

    printing alphabetically charaters in orders of triangle shape



    for (char i = 'A'; i <= 'Z'; i++) {
    for (char j = 'Z'; j >i; j--) {
    System.out.print(" ");
    }
    for (char l = 'A'; l < (2*i-64); l++) {
    System.out.print(i+"");
    }
    System.out.println(" ");
    }

    ReplyDelete
  92. Image

    1
    121
    12321
    1234321

    How to do this pattern ?? //Actually it is an equilateral triangle type pattern

    ReplyDelete
  93. Image

    I need pattern of
    *#*#*
    *#*#
    *#*
    *#
    *

    ReplyDelete
  94. Image

    149
    28598
    7864
    189
    12
    How solve this problem

    ReplyDelete
  95. Image

    How to print that pattern(excluding '-'):

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

    ReplyDelete
  96. Image

    Guys help needed
    1 5
    12 45
    123 345
    1234 2345
    1234512345

    ReplyDelete
  97. Image

    how to print the pattern :

    *
    * *
    * * *
    * *
    *

    ReplyDelete
  98. Image

    Hi, here i represents row, and j represents column but what represents k?

    ReplyDelete
  99. Image

    How to print pattern like this-:
    int array[]={4,1,2,4,3};

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

    ReplyDelete
  100. Image
  101. Image

    how can print this pattern:
    1
    101
    10001
    100001
    1000001

    ReplyDelete
  102. Image
  103. Image
  104. Image

    * * * * *
    * * *
    *
    * * *
    * * * * *
    pattern program in c

    ReplyDelete
  105. Image

    print this pls
    1
    21
    321
    4321
    54321

    ReplyDelete
  106. Image

    how to print this pattern
    1
    21
    321
    4321
    54321

    ReplyDelete
  107. Image

    The method takes an integer argument and prints the following pattern, shown n = 4.

    abcdcba
    abc cba
    ab ba
    a a
    ab ba
    abc cba
    abcdcba

    ReplyDelete
  108. Image

    Write a program for the below given pattern:
    11
    11 10 11
    11 10 9 10 11
    11 10 9 8 9 10 11

    Can somebody please help me?

    ReplyDelete
  109. Image

    can anyone do this program please.

    **0**

    *0*0*

    0*0*0

    *0*0*

    **0**

    ReplyDelete
  110. Image

    can anyone do this program please

    1 2 3 4 3 2 1
    1 2 3 * 3 2 1
    1 2 * * * 2 1
    1 * * * * * 1

    ReplyDelete
  111. Image
  112. Image

    1
    2 3
    4 5 6
    7 8 9 10
    please solve this

    ReplyDelete
  113. Image

    hello brother and sister i just have a problem on solving this?
    *********
    *********
    *********
    *********
    *********
    *********

    ReplyDelete
  114. Image

    ********
    ********
    ********
    ********
    how is this?

    ReplyDelete
  115. Image

    how to print this kind of pattern i need hel guys?


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

    ReplyDelete
    Replies
    1. Image

      public class Demo {

      public static void main(String[] args) {
      int i,j,k;
      for(i=5;i>=1;i--)
      {
      for(j=1;j<=i;j++)
      {
      System.out.print("*");
      }
      }
      }
      }

      Delete
  116. Image

    how to print this ? help me please
    *****
    *****
    *****
    *****
    *****

    ReplyDelete
  117. Image

    http://javaconceptoftheday.com/number-pattern-programs-in-java/

    check link bro there is 20 numbers pattern which will help u lot

    ReplyDelete
  118. Image

    13579
    35791
    57913
    79135
    91357

    ReplyDelete
  119. Image

    What will be the Program..??
    1 0 0 0 0 0 0 1
    3 3 0 0 0 0 3 3
    5 5 5 0 0 5 5 5
    7 7 7 7 7 7 7 7
    5 5 5 0 0 5 5 5
    3 3 0 0 0 0 3 3
    1 0 0 0 0 0 0 1

    ReplyDelete
  120. Image

    1 3 5 7 9 11 13
    * 2 4 6 8 10 *
    * * 3 5 7 * *
    * * * 4 * * *
    * * 3 5 7 * *
    * 2 4 6 8 10 *
    1 3 5 7 9 11 13

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.