Java pattern & pyramid programs

Here we will learn how to write pattern and pyramid programs in java.

Pattern 1:

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

Program:


Public class FirstPatternProgram
{
Public static void main(String[] arg)
{
Int rows=6;
for(int i=1;i<=rows;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(k+” “);
}
System.out.println( );
}
}
}

Pattern 2:

1
2 3
4 5 6
7 8 9 10
…..so on
This is also known Floyd's triangle

Program:


Public class SecondPatternProgram
{
Public static void main(String[] arg)
{
Int a=1,rows=4;
for(int i=1;i<=rows;i++)
{
for(int k=1;k<=i;i++)
{
System.out.print(a+ “ “);
a++;
}
System.out.println( );
}
}
}

Pattern 3:

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

Program:


Public class ThirdPatternProgram
{
Public static void main(String[] arg)
{
Int rows=5;
for(int i=rows;i>=1;i--)
{
for(int k=1;k<=i;k++)
{
System.out.print(* );
}
System.out.println( );
}
}
}

Pattern 4:

#This is pascal’s triangle
                     1
                  1     1
                1    2    1
              1    3    3   1
             1  4     6    4  1

Program:


Public class FourPatternProgram
{
Public static void main(String[] arg)
{
Int rows=5;
for(int i=0;i<rows;i++)
{
 for (int j= 0; j < rows - i; j++)
{
System.out.print(" ");
 }               	          
Int number=1;
for(int k=0;k<=i;k++)
{
 System.out.print(number + " ");
               
number=number*(i-j)/(j+1);
}
System.out.println( );
}
}
}


Pattern 5:

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

Program:


Public class FivePatternProgram
{
Public static void main(String[] arg)
{
Int rows=5;
for(int i=1;i<rows;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(i+" ");
}
System.out.println( );
}
}
}

Pattern 6:

A
B B
C C C
D D D D
E E E E E
F F F F F F

Program:


Public class SixPatternProgram
{
Public static void main(String[] arg)
{
Int rows=6;
Char c=’A’;
for(int i=1;i<=rows;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(ch+" ");
}
c++;
System.out.println( );
}
}
}

Pattern 7:

A
B C
D E F
G H I J
K L M N O

Program:

 
Public class SevenPatternProgram
{
Public static void main(String[] arg)
{
Int rows=5;
Char c=’A’;
for(int i=1;i<=rows;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(ch+" ");
c++;
}
System.out.println( );
}
}
}

Pattern:8

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

program:

Public class EightPatterProgram
{
Public static void main(String[] arg)
{
Int rows=5
for(int i=0;i<rows;i++)
{
for(k=2*(rows-1);k>=0;K--)
{   
System.out.print(“ “);
}
for(j=0;j<=i;j++)
{
System.out.print(“* “);
}
System.out.println( );
}
}
}

Pattern:9


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

program:

Public class NinePatterProgram
{
Public static void main(String[] arg)
{
Int rows=5;
Int num=rows-1;
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=num;j++)
{
System.out.print(“ “);
}
Num--;
for(int k=1;k<2*i-1;k++)
{
System.out.print(“* “);
}
System.out.println( );
}
}
}

pattern :10

                           1
                           10
                           101
                           1010
                           10101

program:


Public class TenPatterProgram
{
Public static void main(String[] arg)
{
Int rows=5;
for(int i=1;i<=rows;i++)
{
for(int k=1;k<=i;k++)
{
if(k%2==0)
{
System.out.print(“0”);
}
else
{
System.out.print(“1”);
}
}
System.out.println( );
}
}
}

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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