Here you will get list of java programs to print patterns of stars, numbers and alphabets. If you want code for any particular pattern then mention it in comment section, I will try to add the program here.
Java Pattern Programs
Patterns of Stars
Pattern 1:
* ** *** **** *****
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= i; ++j) System.out.print("*"); System.out.print("\n"); } } }
Pattern 2:
* ** *** **** *****
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 4; j >= i; --j) System.out.print(" "); for(int k = 1; k <= i; ++k) System.out.print("*"); System.out.print("\n"); } } }
Pattern 3:
***** **** *** ** *
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 5; j >= i; --j) System.out.print("*"); System.out.print("\n"); } } }
Pattern 4:
***** **** *** ** *
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j < i; ++j) System.out.print(" "); for(int k = 5; k >= i; --k) System.out.print("*"); System.out.print("\n"); } } }
Pattern 5:
* *** ***** ******* *********
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 5; j > i; --j) System.out.print(" "); for(int k = 1; k < (i*2); ++k) System.out.print("*"); System.out.print("\n"); } } }
Pattern 6:
********* ******* ***** *** *
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j < i; ++j) System.out.print(" "); for(int k = 10; k >= (i*2); --k) System.out.print("*"); System.out.print("\n"); } } }
Pattern 7:
* * * * * * * *********
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 5; j > i; --j) System.out.print(" "); for(int k = 1; k < (i*2); ++k) if(k==1 || k==(i*2)-1 || i==5) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } } }
Pattern 8:
***** ***** ***** ***** *****
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= 5; ++j) System.out.print("*"); System.out.print("\n"); } } }
Pattern 9:
***** * * * * * * *****
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= 5; ++j) if(j==1 || j==5 || i==1 || i==5) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } } }
Pattern 10:
********* **** **** *** *** ** ** * * * * ** ** *** *** **** **** *********
public class PrintPattern { public static void main(String args[]) { int i,j,k,m,n=5; for(i=1,m=-1;i<=n;++i,m+=2){ if(i==1) for(j=1;j<n*2;++j) System.out.print("*"); else{ for(k=i;k<=n;++k) System.out.print("*"); for(k=1;k<=m;++k) System.out.print(" "); for(k=i;k<=n;++k) System.out.print("*"); } System.out.print("\n"); } for(i=1,m=n*2-3;i<=n;++i,m-=2){ if(i==n) for(j=1;j<n*2;++j) System.out.print("*"); else{ for(k=1;k<=i;++k) System.out.print("*"); for(k=1;k<=m;++k) System.out.print(" "); for(k=1;k<=i;++k) System.out.print("*"); } System.out.print("\n"); } } }
Patterns of Numbers
Pattern 1:
1 12 123 1234 12345
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= i; ++j) System.out.print(j); System.out.print("\n"); } } }
Pattern 2:
1 22 333 4444 55555
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= i; ++j) System.out.print(i); System.out.print("\n"); } } }
Pattern 3:
1 21 321 4321 54321
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 1, k=i; j <= i; ++j, --k) System.out.print(k); System.out.print("\n"); } } }
Pattern 4:
This pattern is also known as pascal’s triangle.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
public class PrintPattern { public static void main(String args[]) { int i,j,k,n=5; for(i=0;i<n;++i){ //loop to print spaces at starting of each row for(j=1;j<=(n-i-1);++j){ System.out.print(" "); } //loop to calculate each value in a row and print it for(k=0;k<=i;++k){ System.out.print(fact(i)/(fact(i-k)*fact(k)) + " "); } System.out.print("\n"); } } //function to calculate factorial static long fact(int x){ int i; long f=1; for(i=1;i<=x;++i){ f=f*i; } return f; } }
Patterns of Alphabets
Pattern 1:
A AB ABC ABCD ABCDE
public class PrintPattern { public static void main(String args[]) { for(int i = 1; i <= 5; ++i) { for(int j = 0; j < i; ++j) System.out.print((char)('A' + j)); System.out.print("\n"); } } }
Pattern 2:
A BB CCC DDDD EEEEE
public class PrintPattern { public static void main(String args[]) { for(int i = 0; i < 5; ++i) { for(int j = 0; j <= i; ++j) System.out.print((char)('A' + i)); System.out.print("\n"); } } }
Pattern 3:
A BA CBA DCBA EDCBA
public class PrintPattern { public static void main(String args[]) { for(int i = 0; i < 5; ++i) { for(int j = 0, k=i; j <= i; ++j, --k) System.out.print((char)('A' + k)); System.out.print("\n"); } } }
Feel free to ask your queries related to above java pattern programs in comment section.
I want this pattern, :
a b
aa bb
aaabbb
class Untitled
{
public static void main(String[ ] args)
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=1;j++)
{
if(i==1)
{
System.out.print("a b");
}
else if(i==2)
{
System.out.print("aa bb");
}
else
{
System.out.print("aaabbb");
}
}
System.out.println( );
}
}
}
Input
N=5
Output :
L
LE
LED
LE
L
How to print this pattern?
How to print
D
A
B A
C B A
D C B A
How to print-
* *
* *
* *
* *
* *
class Notepad
{
public static void main(String[ ] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=1;j++)
{
System.out.print("**");
}
System.out.println( );
}
}
}
A
B D
E H K
Input: PROGRAM
Output:
G
GR
GRA
GRAM
GRAMP
GRAMPR
GRAMPRO
I want … this pattern
A
AB
ABA
ABAB
ABABA
Can you please solve this:
a
a 1
a 2 a
a 3 a 4
a 5 a 6 a
int max=10;
int counter=0;
int k=1;
for(int i=0;i<max;i++)
{
for(int j=0;j<=i;j++)
{
if(j%2==0)
{
System.out.print("a");
}
else
{
System.out.print(k);
k+=1;
}
}
System.out.println();
}
How to do this program.???
1234321
123 321
12 21
1 1
I want this a-b-c patterns con some one help ,me for this, i need code.
Comment a sample pattern of which u need code of
*
1 2
* * *
1 2 3 4
Send me code
ABC
ABC
ABC
program for the below pattern??
AAAA1
AAA12
AA123
A1234
12345
How to do this program?
Thank you
*** *** ** *** **
** ** ** ** ** ** ** **
** *** ** ** ** ** **
** * ** ** ** ** **
** ** ** ** ** **
** ** **** ****
how to make this pattern??
*
*A*
*A*A*