NUMBER PATTERN IN JAVA

EX 1:

//OUTPUT
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
//


public class Pattern1 {

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

Ex 2:

//Output
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
//

public class Pattern2 {

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

Ex 3:

//OUTPUT
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
//

public class Pattern3 {

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

Ex 4:

//OUTPUT
    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5 
//

public class Pattern4 {

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

Ex 5:

//OUTPUT
    5 
   4 4 
  3 3 3 
 2 2 2 2 
1 1 1 1 1 
//

public class Pattern5 {

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

Ex 6:

//OUTPUT
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5 
//

public class Pattern6 {

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

Ex 7:

//OUTPUT
1 2 3 4 5 
 2 3 4 5 
  3 4 5 
   4 5 
    5 
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5 
//

public class Pattern7 {

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

Ex 8:

//OUTPUT
    5 
   4 5 
  3 4 5 
 2 3 4 5 
1 2 3 4 5 
 2 3 4 5 
  3 4 5 
   4 5 
    5 
//

public class Pattern8 {

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

Ex 9:

//OUTPUT
     1 
    1 2 
   1 2 3 
  1 2 3 4 
 1 2 3 4 5 
  2 3 4 5 
   3 4 5 
    4 5 
     5 
//

public class Pattern9 {

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

Ex 10:

//OUTPUT
     1 
    1 2 
   1 2 3 
  1 2 3 4 
 1 2 3 4 5 
  1 2 3 4 
   1 2 3 
    1 2 
     1 
//

public class Pattern10 {

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

Ex 11:

//OUTPUT
1                 1 
1 2             2 1 
1 2 3         3 2 1 
1 2 3 4     4 3 2 1 
1 2 3 4 5 5 4 3 2 1 
//

public class Pattern11 {

	public static void main(String[] args) 
	{		
		for(int row = 1;row<=5;row++)
		{
			for(int col = 1; col<=row; col++)
			{
				System.out.print(col + " ");
			}
			for(int space = row; space<5; space++)
			{
				System.out.print(" " + " ");
			}			
			for(int space = 5; space>row;space--)
			{
				System.out.print(" " + " ");	
			}
			for(int col = row; col>= 1;col--)
			{
				System.out.print(col + " ");	
			}
			System.out.println();
		}
	}
}

Ex 12:

//OUTPUT
5 4 3 2 1 1 2 3 4 5 
5 4 3 2     2 3 4 5 
5 4 3         3 4 5 
5 4             4 5 
5                 5 
//

public class Pattern12 {

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

Ex 13:

//OUTPUT
2 
4 6 
8 10 12 
14 16 18 20 
22 24 26 28 30 

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

public class Pattern13 {
	static int a = 0;
	static int b = 1;
	
	public static void main(String[] args) 
	{	
		Pattern13 obj = new Pattern13();
		obj.method1();
		System.out.println();
		obj.method2();		
	}
	
	public void method1()
	{
	for(int row = 1;row<=5;row++)
	{
		for(int col = 1; col<=row; col++)
		{				
			System.out.print((a=a+2) + " ");
		}
		System.out.println();
	}
	}
	
	public void method2()
	{
	for(int row = 1;row<=5;row++)
	{
		for(int col = 1; col<=row; col++)
		{				
			System.out.print((b++) + " ");
		}
		System.out.println();
	}
	}
}

Ex 14:

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

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

		for (int row = 1; row <= 5; row++) 
        {
            int num = row;
            for (int col = 1; col <= row; col++) 
            {
                System.out.print(num+" ");                 
                num = num+5-col;
            }             
            System.out.println();
        }
		
	}
}

Ex 15:

//OUTPUT
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
//

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

		for(int row=1; row<=7; row++)
		{
			for(int col = 1; col<row; col++)
			{
				System.out.print(col + " ");
			}
			for(int col = row; col>=1; col--)
			{
				System.out.print(col + " ");
			}
			System.out.println();
		}
		
	}
}

Ex 16:

//OUTPUT
1 2 3 4 5 6 7 
  2 3 4 5 6 7 
    3 4 5 6 7 
      4 5 6 7 
        5 6 7 
          6 7 
            7 
          6 7 
        5 6 7 
      4 5 6 7 
    3 4 5 6 7 
  2 3 4 5 6 7 
1 2 3 4 5 6 7 

//

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

		for(int row=1; row<7; row++)
		{
			for(int col = 1; col<row; col++)
			{
				System.out.print(" " + " ");
			}
			for(int col = row; col<=7; col++)
			{
				System.out.print(col + " ");
			}
			System.out.println();
		}
		for(int row=7; row>=1; row--)
		{
			for(int col = 1; col<row; col++)
			{
				System.out.print(" " + " ");
			}
			for(int col = row; col<=7; col++)
			{
				System.out.print(col + " ");
			}
			System.out.println();
		}
		
	}
}

Ex 17:

//OUTPUT
1 
1 0 
1 0 1 
1 0 1 0 
1 0 1 0 1 
1 0 1 0 1 0 
1 0 1 0 1 0 1 
//

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

		for(int row=1; row<=7; row++)
		{
			for(int col = 1; col<=row; col++)
			{
				if(col%2==0)
				{
				System.out.print("0 ");
				}
				else
				{
				System.out.print("1 ");
				}
			}
			System.out.println();
		}		
	}
}

Ex 18:

//OUTPUT
1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 0 1 
//

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

		for(int row=7; row>=1; row--)
		{
			for(int col = row; col<=7; col++)
			{
				if(col%2==0)
				{
				System.out.print("0 ");
				}
				else
				{
				System.out.print("1 ");
				}
			}
			System.out.println();
		}		
	}
}

Leave a comment

Design a site like this with WordPress.com
Get started