Sunday, September 8, 2024

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

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?

481 comments:

  1. 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. 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. ... 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. sir pls print this for me
      **0**

      *0*0*

      0*0*0

      *0*0*

      **0**

      Delete
    4. 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
  2. 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. for(int i=1; i<=5; i++)
      {
      int countr = 1;
      while(i>=countr)
      {
      System.out.print(i + " ");
      countr++;
      }

      System.out.println("");
      }

      Delete
    2. how to write program to display
      ####
      ###
      ##
      #

      Delete
    3. 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. for (int i = 4; i >= 1; i--) {

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

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

      System.out.println();
      }

      Delete
    5. what is the code to get output as
      * *
      ** **
      *** ***
      **** ****
      *********

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

    ReplyDelete
    Replies
    1. 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. Can anyone say how to print two star pyramid with spaces

    ReplyDelete
    Replies
    1. 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. Display the code for this:

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

    ReplyDelete
    Replies
    1. 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. It was very simple

      Delete
    3. 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. 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. 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. Display the code for this:

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

    ReplyDelete
    Replies
    1. 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. *
    **
    ***
    ****
    *****
    print this with only two loops

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

      System.out.println();
      }

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

      Delete
  8. No I'm as asking for two pyramids not for triangles

    ReplyDelete
  9. I asked for double pyramids not triangle

    ReplyDelete
  10. 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. 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. 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. 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. 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. 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. How to print these patterns:
    (i)
    *
    *
    *
    *
    *
    (ii)
    * *
    * *
    **
    * *
    * *

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

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

      Delete
    3. (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. 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. (i)
    for(i=0;<5;i++)
    {
    for(j=0;j<1;j++)
    System.out.print(" * ");
    System.out.println();

    }

    ReplyDelete
    Replies
    1. compilaction error..!!
      bcz illegal start of type

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

      Delete
  13. Any one please help me to send below Java pattern program on my email id
    singhkar1387@gmail.com

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

    ReplyDelete
    Replies
    1. 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. you r rocking bro

      Delete
    3. Class test
      {
      Public ststic void main(string args)
      {
      For(i=1;i<=5;i++)
      {
      For(j=1;j<=i;j++)
      {
      If(i%2==0)
      {
      System.out.println("+");
      }else{
      System.out.println("*");
      }
      }
      System.out.println(/n);
      }
      }
      }
      }

      Delete
  14. 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. Replies
    1. 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. please solve this below two problems
    (i)12345
    1234
    123
    12
    1
    (ii)5555
    4444
    3333
    2222
    1111

    ReplyDelete
    Replies
    1. 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. 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. 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. 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. how to print this
    *****
    * *
    * *
    * *
    *****

    ReplyDelete
    Replies
    1. 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. 1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

    how to create

    ReplyDelete
    Replies
    1. 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. 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. while(cnt=0)----- may be it's error.....!!!....???

      Delete
    4. 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. 1
    12
    123
    1234
    12345
    1234
    123
    12
    1

    plz can any one give code for this program

    ReplyDelete
    Replies
    1. 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. 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. 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. * *
    * * * *
    * * * * * *
    * * * * * * * *
    how to print this pattern in java ,sonupatel984@gmail.com

    ReplyDelete
    Replies
    1. 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. Hi how can you display this output?

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

    when the input is 5?

    ReplyDelete
    Replies
    1. 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. 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. 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. 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. How do you print following pyramid pattern of stars in Java:

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

    ReplyDelete
    Replies

    1. 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. 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. Can you write Java code for the given program. Ex: jayanthraman94@gmail.com replace all the characters with '*'except'j'and '@gmail.com' output: j*****.....@gmail.com

    ReplyDelete
  26. Write a Java prg given below. Input: jayanthraman@gmail.com. output: j******....@gmail.com

    ReplyDelete
  27. Can you write java program for this pattern.

    ReplyDelete
  28. 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. Hi how can you display this output?
    abcde abcde
    abcd bcde
    abc cde
    ab de
    a e

    ReplyDelete
    Replies
    1. 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
  30. please help me with this....

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

    ReplyDelete
    Replies
    1. 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. 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. 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. 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. 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. This comment has been removed by the author.

    ReplyDelete
  34. how to code this
    *
    **
    ***
    ****
    *****
    ******
    *
    *
    ***

    ReplyDelete
  35. How to print the below pattern
    *!
    *! *-
    *! *- *!
    *! *- *! *-
    *! *- *! *- *!

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

    ReplyDelete
  37. 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
  38. how to print
    *
    * *
    * * *
    * * * *
    * * *
    * *
    *

    ReplyDelete
    Replies
    1. 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. 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
  39. 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. /* 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
  40. 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. 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
  41. 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
  42. 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. 00
    1001
    010010
    10100101
    0101001010
    101010010101
    101010010101
    0101001010
    10100101
    010010
    1001
    00

    ReplyDelete
  44. print this please using loops

    *
    *
    * *
    * *
    * * *

    ReplyDelete
    Replies
    1. 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
  45. *
    **
    ***
    **
    * give me the program pls
    *

    ReplyDelete
  46. How to print
    A
    AB
    ABC
    ABCD
    ABCDE

    ReplyDelete
  47. please help this pattern
    *
    *-*
    *-*-*
    *-*-*-*

    ReplyDelete
  48. 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
  49. * *
    ** **
    *****
    ** **
    * *

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

    ReplyDelete
  50. Replies
    1. 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. in first line, it will print 0

      Delete
  51. 1
    22 22
    333 333 333
    4444 4444 4444 4444

    ReplyDelete
    Replies
    1. /*
      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
  52. i want a pattern of boat ....can anyone help me...pattern should like..

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

    ReplyDelete
  53. Replies
    1. 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
  54. Hi how can you display this output?

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

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

    ReplyDelete
  55. How to print below java pattern?
    1
    72
    863
    10954


    ReplyDelete
  56. * * *
    * * * * *
    * * * * *
    * *** *
    *

    ReplyDelete
  57. HOW To Print??

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

    ReplyDelete
    Replies
    1. 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
  58. Hi how to print *
    *****
    ***
    *
    Mean to say asterik symbol with asteriks

    ReplyDelete
    Replies
    1. How to print
      * *
      * *
      *
      * *
      * *

      Pls help

      Delete
    2. 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
  59. can u pls tell me how to print
    *****
    * *
    * *
    * *
    *****

    ===A Hollow square??

    ReplyDelete
  60. Replies
    1. 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
  61. ***********
    ***** *****
    **** ****
    *** ***
    ** **
    * *
    ** **
    *** ***
    **** ****
    ***** *****
    ***********

    ReplyDelete
  62. 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
  63. sir how to print

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

    ReplyDelete
  64. what about this?
    ABCDEF
    BCDEF
    CDEF
    DEF
    EF
    F

    ReplyDelete
  65. 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
  66. how can print this pattern:
    1
    21
    321
    4321
    54321

    ReplyDelete
  67. How to print this pattern

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

    in Java (excluding that dashes)

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

    ReplyDelete
  69. 1
    12
    123
    1234
    123
    12
    1
    please solve this?

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

    ReplyDelete
    Replies
    1. 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
  71. how to print this pattern pls ... i need it badly please
    * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *
    using for and while loop with bufferedreader?

    ReplyDelete
  72. 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
  73. 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
  74. 1
    1 2 1
    1 2 4 2 1
    1 2 4 8 4 2 1

    ReplyDelete
  75. 1
    12
    123
    1234
    12345
    How to print this output?

    ReplyDelete
  76. 1
    12
    123
    1234
    12345
    How to print this output??

    ReplyDelete
    Replies
    1. 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. 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
  77. 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
  78. 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
  79. hi does anyone know ho to print a triangle with a shape chosen by the user ?? (with textIO or scanner) thanks alot

    ReplyDelete
  80. Help me in printing this:
    1
    32
    456
    10987
    1112131415

    ReplyDelete
  81. how to printing this? plz help

    1
    22
    333
    4444
    55555

    ReplyDelete
    Replies
    1. 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
  82. 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
  83. How to print that pattern(excluding '-'):

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

    Thanks in advance.

    ReplyDelete
    Replies
    1. 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
  84. 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
  85. 1
    121
    12321
    1234321

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

    ReplyDelete
  86. I need pattern of
    *#*#*
    *#*#
    *#*
    *#
    *

    ReplyDelete
  87. 149
    28598
    7864
    189
    12
    How solve this problem

    ReplyDelete
  88. How to print that pattern(excluding '-'):

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

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

    ReplyDelete
  90. how to print the pattern :

    *
    * *
    * * *
    * *
    *

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

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

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

    ReplyDelete
  93. how can print this pattern:
    1
    101
    10001
    100001
    1000001

    ReplyDelete
  94. * * * * *
    * * *
    *
    * * *
    * * * * *
    pattern program in c

    ReplyDelete
  95. print this pls
    1
    21
    321
    4321
    54321

    ReplyDelete
  96. how to print this pattern
    1
    21
    321
    4321
    54321

    ReplyDelete
  97. 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
  98. 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
  99. can anyone do this program please.

    **0**

    *0*0*

    0*0*0

    *0*0*

    **0**

    ReplyDelete
  100. 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
  101. 1
    2 3
    4 5 6
    7 8 9 10
    please solve this

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

    ReplyDelete
  103. ********
    ********
    ********
    ********
    how is this?

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


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

    ReplyDelete
    Replies
    1. 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
  105. how to print this ? help me please
    *****
    *****
    *****
    *****
    *****

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

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

    ReplyDelete
  107. 13579
    35791
    57913
    79135
    91357

    ReplyDelete
  108. 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
  109. 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.