DEV Community

vimala jeyakumar
vimala jeyakumar

Posted on

2

For loop - Alphabet pattern printing

Program 1:

package Forloop;

public class PatternV
{
        public static void main(String[] args) 
        {
            patternV();

        }

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

                System.out.println();  
            }

        }
    }


------------------------------------------------------------------
OUTPUT:

*               * 
  *           *   
    *       *     
      *   *       
        *    
Enter fullscreen mode Exit fullscreen mode

Program 2:

package Forloop;

public class PatternI 
{

    public static void main(String[] args) 
    {
        patterni();

    }

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

            System.out.println(); 
        }
    }

}

--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Program 3:

package Forloop;

public class PatternM 
{

    public static void main(String[] args) 
    {
        patternM();


    }
    private static void patternM()
    {

        int line;
        for ( line=1; line<=9;line++)
        {
            for (int col=1; col<=9;col++)
            {
                if (col==1 || col==9)
                  System.out.print("* ");   
                else if((line==col || line+col==10)&&line<=5)
                       System.out.print("* ");
                 else
                     System.out.print("  ");
                }

            System.out.println(); 
        }

    }

    }


--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Program 4:

package Forloop;

public class PatternA
{

    public static void main(String[] args) 
    {
        patternA();

    }

    private static void patternA() 
    {
        for (int line=1; line<=9;line++)
        {
            for(int col=1; col<=9; col++) 
            {
                if((line==col || line+col==10) && 5<=line)
                     System.out.print("* ");
                else if( (line==7 ||col==3||col==4|| col==5)&& 7==line)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }

            System.out.println();  
        }

    }

}
--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Program 5:

package Forloop;

public class PatternL
{

    public static void main(String[] args) 
    {
        patternL();

    }

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

            System.out.println(); 
        }
    }

}
--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Program 6:

package Forloop;

public class PatternD {

    public static void main(String[] args) 
    {
        patternD();
    }

    private static void patternD()
    {   
        for (int line=1; line<=10;line++)
            {
               for(int col=1; col<=10; col++) 
                 {
                    if(col==3||col==10)
                          System.out.print("* "); 
                    else if(line==10 || line==1)
                           System.out.print("* ");
                     else
                         System.out.print("  ");
            }

                        System.out.println(" "); 
                    }

    }

}
--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Program 7:

package Forloop;

public class PatternE {

    public static void main(String[] args) 
    {
            patternE();
            patternV();
            patterni();

    }

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

            System.out.println(); 
        }
    }

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

            System.out.println();  
        }

    }

        private static void patternE() 
        {

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

                System.out.println("  "); 
            }


    }

}

--------------------------------------------------------------------
OUTPUT:

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


*               * 
  *           *   
    *       *     
      *   *       
        *       


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


Enter fullscreen mode Exit fullscreen mode

Program 8:(F)

package Forloop;

public class PatternF {

    public static void main(String[] args) 
    {
        patternF();

    }

    private static void patternF() 
    {

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

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

--------------------------------------------------------------------
OUTPUT:

* * * * *   
*   
*   
*   
* * * * *   
*   
*   
*   
*   
*   
Enter fullscreen mode Exit fullscreen mode

Program 9:

package Forloop;

public class Pattern1
{

    public static void main(String[] args) 
    {
        pattern1();
        pattern2();

    }

    private static void pattern2() 
    {
        for (int line=1; line<=10;line++)
        {
           for(int col=1; col<=10; col++) 
             {
                if(col==1||col==10)
                      System.out.print("* "); 
                else if(line==col||line+col==10)
                       System.out.print("* ");
                 else
                     System.out.print("  ");
               }

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


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

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

}


--------------------------------------------------------------------
OUTPUT:

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


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

Enter fullscreen mode Exit fullscreen mode

Program 10:

package Forloop;

public class Pattern2 {

    public static void main(String[] args) 
    {
        pattern2();

    }

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

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

    }


--------------------------------------------------------------------
OUTPUT:

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

Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more