Pattern Programs
Pattern 1:
package patternprograms;
import java.util.Scanner;
public class Pattern1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
System.out.print(" * ");
}
System.out.println("");
}
}
}
Enter the number
5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Pattern 2:
package patternprograms;
import java.util.Scanner;
public class Pattern2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" * ");
}
System.out.println("");
}
}
}
Enter the number
5
*
* *
* * *
* * * *
* * * * *
Pattern 3:
package patternprograms;
import java.util.Scanner;
public class Pattern3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(i==1||i==n||j==1||j==n)
{
System.out.print(" * ");
}
}
System.out.println("");
}
}
}
Enter the number
5
*
*
*
*
* * * * *
Pattern 4:
package patternprograms;
import java.util.Scanner;
public class Pattern4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Enter the number
5
* * * * *
* *
* *
* *
* * * * *
Pattern 5:
package patternprograms;
import java.util.Scanner;
public class Pattern5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==j||i+j==n+1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Enter the number
5
*
* *
* *
*
* *
* *
Pattern 6:
package patternprograms;
import java.util.Scanner;
public class Pattern6
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i==(n/2)+1||j==(n/2)+1)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
Enter the number
5
*
*
*
* * * * * *
*
*
Pattern 7:
package patternprograms;
import java.util.Scanner;
public class Pattern7
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(" *");
}
System.out.println(" ");
}
}
}
Enter the number
5
* * * * *
* * * *
* * *
* *
*
Pattern 8:
package patternprograms;
import java.util.Scanner;
public class Pattern8
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=n-i;j>=1;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("* ");
}
System.out.println("");
}
}
}
Enter the number
5
*
* *
* * *
* * * *
* * * * *
Pattern 9:
package patternprograms;
import java.util.Scanner;
public class Pattern9
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=n;k>i;k--)
{
System.out.print(" *");
}
System.out.println(" ");
}
}
}
Enter the number
5
* * * *
* * *
* *
*