Pattern Printing Program
Program to print most possible patterns in Blue J
Paste this program in a class editor source code of a Blue J File :-
_________________________________________________________________________________
import java.util.Scanner;
class Patterns
{
Scanner S = new
Scanner(System.in);
public void floyd()
{
int k =
1;
Patterns
ob = new Patterns();
for(int i
= 1; i<=5;i++)
{
for(int u =1;u<=i;u++)
{
System.out.print(k+" ");
k++;
}
System.out.println();
}
ob.main();
}
public void
wordPattern()
{
System.out.println("Enter the String to print its Pattern");
String
str = S.next();
str=str.toUpperCase();
Patterns
ob = new Patterns();
int len =
str.length();
System.out.println("Enter the pattern you want to print");
System.out.println("1) D");
System.out.println(" RD");
System.out.println(" ORD");
System.out.println(" WORD");
System.out.println("2) I");
System.out.println(" IC");
System.out.println(" ICS");
System.out.println(" ICSE");
int ch =
S.nextInt();
if(ch==1)
{
for(int i =1;i<=len;i++)
System.out.println(str.substring((len-i),len));
}
if(ch==2)
{
for(int i = 1; i<=len;i++)
System.out.println(str.substring(0,i));
}
ob.main();
}
public void triangles()
{
System.out.println("Enter the no of rows you want in the triangle");
int rows
= S.nextInt();
Patterns
ob = new Patterns();
System.out.println("Press 1 for Triangle");
System.out.println("Press 2 for Inverted Triangle");
System.out.println("Enter your choice :-");
int ch =
S.nextInt();
if(ch==1)
{
for(int i = 1;i<=rows;i++)
{
for(int j= 1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}
}
if(ch==2)
{
for(int i = rows;i>=1;i--)
{
for(int j = 1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}
}
else
System.out.println("Sorry, Wrong choice Entered!");
ob.main();
}
public void
reversetriangles()
{
System.out.println("Enter the no of rows you want in the triangle");
int rows
= S.nextInt();
Patterns
ob = new Patterns();
System.out.println("Press 1 for Triangle");
System.out.println("Press 2 for Inverted Triangle");
System.out.println("Enter your choice :-");
int ch =
S.nextInt();
if(ch==1)
{
for(int i = 1;i<=rows;i++)
{
for(int j=rows;j>rows-i;j--)
System.out.print(j+" ");
System.out.println();
}
for(int i = 1;i<=rows;i++)
{
for(int j=rows;j>rows-i;j--)
System.out.print(i+" ");
System.out.println();
}
}
if(ch==2)
{
for(int i = rows;i>=1;i--)
{
for(int j = rows;j>rows-i;j--)
System.out.print(j+" ");
System.out.println();
}
for(int i = rows;i>=1;i--)
{
for(int j = rows;j>rows-i;j--)
System.out.print(i+" ");
System.out.println();
}
}
else
System.out.println("Sorry, Wrong choice Entered!");
ob.main();
}
void pascal(int x)
{
int[] arr=new int[x];
arr[0]=1;
int i,j,k;
for(i=0;i<x;i++)
{
for(j=i-1;j>=0;j--)
arr[j+1]=arr[j+1]+arr[j];
for(k=0;k<=i;k++)
System.out.print(arr[k]+" ");
System.out.print("\n");
} Patterns ob = new Patterns();
{
int[] arr=new int[x];
arr[0]=1;
int i,j,k;
for(i=0;i<x;i++)
{
for(j=i-1;j>=0;j--)
arr[j+1]=arr[j+1]+arr[j];
for(k=0;k<=i;k++)
System.out.print(arr[k]+" ");
System.out.print("\n");
} Patterns ob = new Patterns();
ob.main();
}
public void main()
{
Scanner
SC = new Scanner(System.in);
Patterns
ob = new Patterns();
System.out.println("Press 1 to print patterns with strings.");
System.out.println("Press
2 to print patterns with numbers.");
System.out.println("Press 3 to exit");
int
choice = SC.nextInt();
if(choice==1)
ob.wordPattern();
if(choice==2)
{
System.out.println("Press 1 for Simple triangles");
System.out.println("Press 2 for Reverse triangles");
System.out.println("Press 3 for Floyd's Triangle");
System.out.println("Press 4 to print Pascal's triangle.");
System.out.println("Press 5 to exit.");
int y = SC.nextInt();
switch(y)
{
case 1: ob.triangles();
break;
case 2: ob.reversetriangles();
break;
case 3: ob.floyd();
break;
case 4: System.out.println("Enter the value of n
");
int x = S.nextInt();
ob.pascal(x);
break;
case 5: System.exit(0);
break;
default : System.out.println("OOPS wrong choice
Entered!!");
System.exit(0);
break;
}
}
if(choice==3)
System.exit(0);
else
System.out.println("Sorry,
Wrong choice Entered!");
System.out.println("");
System.out.println("");
}
}
------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x--
If there are any questions regarding this program's logic, please leave a comment below...