Pattern program in for loop

Pattern 1:-

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

Program:-

package forloop;

public class For1 {

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

	}

}

Output Screenshot:-

Pattern 2:-

54321
5432
543
54
5

Program:-

package forloop;

public class For2 {

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

	}

}

Output Screenshot:-

Pattern 3:-

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

Program:-

package forloop;

public class For3 {

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

}

Pattern 4:-

55555
4444
333
22
1

Program:-

package forloop;

public class For4 {

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

	}

}

Output Screenshot:-

Pattern 5:-

4 4 4 4
3 3 3
2 2
1

Program:-

package forloop;

public class For5 {

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

	}

}

Output Screenshot:-

Pattern 6:-

1 2 3 4
5 6 7
8 9
10

Program:-

package forloop;

public class For6 {

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


Output Screenshot:-

Pattern 7:-

        * 
      * * 
    * * * 
  * * * * 

Program:-

package forloop;

public class For7 {

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

	}

}

Output Screenshot:-

Pattern 8:-

          4
         33
        222
       1111

Program 8:-

package forloop;

public class For8 {

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

	}

}

Output Screenshot:-

Pattern 9:-

       1
      12
     123
    1234

Program:-

package forloop;

public class For9 {

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

}

Output Screenshot:-

Pattern 10:-

     1
    12
  123
1234

Program:-

package forloop;

public class For10 {

	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 star=1;star<=5-row;star++){
		System.out.print(star); }
		System.out.println(); 
		} 
	}
}

Output Screenshot:-

Pattern 11:-

         1
        12
      123
    1234
  12345

Program:-

package forloop;

public class For11 {

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

}

Output Screenshot:-

Pattern 12:-

         5
        44
      333
    2222
  11111

Program:-

package forloop;

public class For12 {

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

	}

}

Output Screenshot:-

Pattern 13:-

       10
      9 8
    7 6 5
  4 3 2 1

Program:-(diff o/p)

package forloop;

public class For13 {

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

	}

}

Output:—

         10
        99
      888
    7777
  66666

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free