Pattern practice 2:

public class Pattern1 {
void add() {
for(int row=5;row>=1;row–)
{
for(int col=1;col<=row;col++)
{
System.out.print(row+” “);

    }    
System.out.println();
}
}
public static void main(String[] args)
{  
  Pattern1 p1=new Pattern1(); 
   p1.add();
}

}

OUTPUT:

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

Pattern 2:

public class Pattern2 {
void add1() {
for(int row=1;row<=5;row++)
{
for(int col=1;col<=row;col++)
{
System.out.print(col+” “);

    }   
 System.out.println();
}
}public static void main(String[] args)
{    
Pattern2 p2=new Pattern2();
    p2.add1();
}

}

OUTPUT:

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

Pattern 3:

public class Pattern3 {
void add2() {
for(int row=5;row>=1;row–)
{
for(int col=5;col>=row;col–)
{
System.out.print(row+” “);

    }    
System.out.println();
}
}public static void main(String[] args)
{  
  Pattern3 p3=new Pattern3(); 
   p3.add2();
}

}

OUTPUT:

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

Pattern 4:

public class Pattern4 {
void add3 {
for(int row=1;row<=5;row++)
{
for(int col=row;col<=5;col++)
{
System.out.print(row+” “);

    } 
   System.out.println();
}
}
public static void main(String[] args)
{    
Pattern4 p4=new Pattern4(); 
   p4.add3();}

}

OUTPUT:

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

Leave a comment

Design a site like this with WordPress.com
Get started