DEV Community

vimala jeyakumar
vimala jeyakumar

Posted on

1

For loop-Numbers pattern printing

Example Program1:

package Forloop;

public class Pattern {

    public static void main(String[] args)
    {
        pattern();
        pattern1();
        pattern2();
        pattern3();
        pattern4();
        pattern5();
        pattern6();

    }

    private static void pattern6()
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if((line==9&&col<9)||(line==5&&col<9)||(line==1&&col>1))
                    System.out.print("* ");
                else if(col==1&&line>1)
                    System.out.print("* ");
                else if((line==6||line==7||line==8)&&col==9)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
         System.out.println();
        }   

    }

    private static void pattern5() 
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if((line==9&&col<9)||(line==5&&col<9)||(line==1&&col>1))
                    System.out.print("* ");
                else if((line==2||line==3||line==4)&&col==1)
                    System.out.print("* ");
                else if((line==6||line==7||line==8)&&col==9)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
         System.out.println();
        }   

    }

    private static void pattern4() 
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if((line==5&&col<8)||col==5||col+line==6)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
         System.out.println();
        }


    }

    private static void pattern3() 
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if(line==9||line==1||(line==5)||col==9)
                    System.out.print("* ");

                else
                    System.out.print("  ");
            }
          System.out.println(); 
        }

    }

    private static void pattern2()
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if((line==9&&col>1)||(line==5&&col>1)||(line==1&&col<9))
                    System.out.print("* ");
                else if((line==2||line==3||line==4)&&col==9)
                    System.out.print("* ");
                else if((line==6||line==7||line==8)&&col==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
         System.out.println();
        }

    }

    private static void pattern1() 
    {
        for (int line=1;line<=9;line++)
        {
            for(int col=1;col<=9;col++)
            {
                if(line==9||col==5||col+line==6)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
         System.out.println();
        }

    }

    private static void pattern() 
    {

        for (int line=1; line<=10;line++)
        {
            for(int col=1; col<=10; col++) {
              System.out.print("*  ");  
            }

            System.out.println("  "); 
        }


    }
}




Enter fullscreen mode Exit fullscreen mode

output:

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

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

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

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

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


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

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

Enter fullscreen mode Exit fullscreen mode

Program2:

package Forloop;

public class Shape {

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

    }
    private static void shape3()
    {
        for (int row=1; row<=5;row++)
        {
          for(int col=1; col<=row; col++)
          {
             System.out.print(col+" " );
          }
        System.out.println();
        }   

    }
    private static void shape2()
    {
        for (int row=1; row<=5;row++)
        {
          for(int col=1; col<=row; col++)
          {
             System.out.print("* ");
          }
        System.out.println();
        }

    }
    private static void shape1() 
    {   
      for (int row = 5; row>=1;row--)
        {
          for(int col=1; col<=row; col++)
          {
             System.out.print("* ");
          }
        System.out.println();
    }

    }
    }



Enter fullscreen mode Exit fullscreen mode

Output:

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

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

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

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

Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay