//File name--> Pattern.java
import java.io.*;
class Pattern{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your choice to print different pattern(1-5):");
int choice=Integer.parseInt(br.readLine());
System.out.println("Enter Number of lines you want:");
int n=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
for(int i=1;i < =n;i++)
{
System.out.println();
for(int j=1;j < =i;j++)
System.out.print(" ");
int a=i;
for(int k=0;k < =n-i;k++)
{
System.out.print(" "+a);
a+=i;
}
}
break;
case 2:
for(int i=0;i < n;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n-i;k++)
System.out.print("* ");
}
break;
case 3:
for(int i=0;i < n/2+1;i++)
{
System.out.println();
for(int j=0;j < n/2-i;j++)
System.out.print(" ");
for(int k=0;k < =i;k++)
System.out.print("* ");
}
for(int i=0;i < n/2;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n/2-i;k++)
System.out.print(" *");
}
break;
case 4:
for(int i=0;i < n;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n-i;k++)
System.out.print("*");
}
break;
case 5:
for(int i=0;i < n/2+1;i++)
{
System.out.println();
for(int j=0;j < n/2-i;j++)
System.out.print(" ");
for(int k=0;k < =i;k++)
System.out.print("*");
}
for(int i=0;i < n/2;i++)
{
System.out.println();
for(int j=0;j < =i;j++)
System.out.print(" ");
for(int k=0;k < n/2-i;k++)
System.out.print("*");
}
break;
default:
System.out.println("Invalid choice");
}
}
}
OUTPUT
javac Pattern.java
java Pattern
Enter your choice to print different pattern(1-5):
1
Enter Number of lines you want:
5
1 2 3 4 5
2 4 6 8
3 6 9
4 8
5
java Pattern
Enter your choice to print different pattern(1-5):
2
Enter Number of lines you want:
5
* * * * *
* * * *
* * *
* *
*
java Pattern
Enter your choice to print different pattern(1-5):
3
Enter Number of lines you want:
7
*
* *
* * *
* * * *
* * *
* *
*
java Pattern
Enter your choice to print different pattern(1-5):
4
Enter Number of lines you want:
6
******
*****
****
***
**
*
java Pattern
Enter your choice to print different pattern(1-5):
5
Enter Number of lines you want:
5
*
**
***
**
*
import java.io.*;
class Pattern{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your choice to print different pattern(1-5):");
int choice=Integer.parseInt(br.readLine());
System.out.println("Enter Number of lines you want:");
int n=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
for(int i=1;i < =n;i++)
{
System.out.println();
for(int j=1;j < =i;j++)
System.out.print(" ");
int a=i;
for(int k=0;k < =n-i;k++)
{
System.out.print(" "+a);
a+=i;
}
}
break;
case 2:
for(int i=0;i < n;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n-i;k++)
System.out.print("* ");
}
break;
case 3:
for(int i=0;i < n/2+1;i++)
{
System.out.println();
for(int j=0;j < n/2-i;j++)
System.out.print(" ");
for(int k=0;k < =i;k++)
System.out.print("* ");
}
for(int i=0;i < n/2;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n/2-i;k++)
System.out.print(" *");
}
break;
case 4:
for(int i=0;i < n;i++)
{
System.out.println();
for(int j=0;j < i;j++)
System.out.print(" ");
for(int k=0;k < n-i;k++)
System.out.print("*");
}
break;
case 5:
for(int i=0;i < n/2+1;i++)
{
System.out.println();
for(int j=0;j < n/2-i;j++)
System.out.print(" ");
for(int k=0;k < =i;k++)
System.out.print("*");
}
for(int i=0;i < n/2;i++)
{
System.out.println();
for(int j=0;j < =i;j++)
System.out.print(" ");
for(int k=0;k < n/2-i;k++)
System.out.print("*");
}
break;
default:
System.out.println("Invalid choice");
}
}
}
OUTPUT
javac Pattern.java
java Pattern
Enter your choice to print different pattern(1-5):
1
Enter Number of lines you want:
5
1 2 3 4 5
2 4 6 8
3 6 9
4 8
5
java Pattern
Enter your choice to print different pattern(1-5):
2
Enter Number of lines you want:
5
* * * * *
* * * *
* * *
* *
*
java Pattern
Enter your choice to print different pattern(1-5):
3
Enter Number of lines you want:
7
*
* *
* * *
* * * *
* * *
* *
*
java Pattern
Enter your choice to print different pattern(1-5):
4
Enter Number of lines you want:
6
******
*****
****
***
**
*
java Pattern
Enter your choice to print different pattern(1-5):
5
Enter Number of lines you want:
5
*
**
***
**
*