Showing posts with label patterns in Java. Show all posts
Showing posts with label patterns in Java. Show all posts

Java program for the pattern of 'I' | Patterns in JAVA | JAVA POINT

 

Java Program for I



Ques - Write a java program for the following pattern of 'I' .


*****

   *

   *

   *

   *

*****



PROGRAM :


import java.util.Scanner;

class I

 {

     public static void main(String []args)

     {

         System.out.println("Enter the length");

         Scanner sc=new Scanner(System.in);

         int i, j, n;

         int r=sc.nextInt();


         for(i=0;i<r;i++)

         {

             for(j=0;j<5;j++)

             {

                 if((i==0)||(i==(r-1)))

                 {

                     System.out.print("*");

                 }    

                 else if(j==2)

                 {

                     System.out.print("*");

                 }    

                 else

                 {

                     System.out.print(" ");

                 }

             }

             System.out.println();

         }

        }

 }


/* Output :

 

Enter the length : 6


*****

   *

   *

   *

   *

*****

 

*/

Java program for the pattern of 'E' | Patterns in JAVA | JAVA POINT

 

Java program for E



Ques : Write a java program for following Pattern of  'E' :


     * * * * 

            *                 

     *         

     * * * * 

     *         

             *                 

     * * * * 


PROGRAM :


import java.util.Scanner;

public class PatternE

{

    public static void main(String[] args)

    {

        System.out.print("Enter the number of      

        rows : ");.                // 7

        Scanner scan=new Scanner(System.in);

        int n=scan.nextInt();

        System.out.println();

        for (int i=0;i<n ;i++ )

        {

            for (int j=0;j<=n/2 ;j++ )

            {

                if (i==0 || i==n-1 || i==n/2 || j==0)

                {

                    System.out.print("* ");

                }

                else

                {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}

Java program for the pattern of 'B' | JAVA Patterns | JAVA POINT

 

Java codes for B

Ques : Write a Java Program for the following pattern 'B'


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


PROGRAM :


import java.util.Scanner;
public class PatternB
{
public static void main(String[] args)
{
System.out.print("Enter the number of
rows : ");. // 9
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println();
for (int i=0;i<n ;i++ )
{
for (int j=0;j<=n/2 ;j++ )
{
if (i==0 && j!=n/2 || j==0 || j==n/2 &&
i!=0 && i!=n/2 && i!=n-1 || i==n/2
&& j!=n/2 || i==n-1 && j!=n/2)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}