Java Pattern Programs of Stars, Numbers and Alphabets
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:
1 2 3 4 5 |
* ** *** **** ***** |
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 4 5 |
* ** *** **** ***** |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 |
***** **** *** ** * |
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 4 5 |
***** **** *** ** * |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 |
* *** ***** ******* ********* |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 |
********* ******* ***** *** * |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 |
* * * * * * * ********* |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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:
1 2 3 4 5 |
***** ***** ***** ***** ***** |
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 4 5 |
***** * * * * * * ***** |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 6 7 8 9 10 |
********* **** **** *** *** ** ** * * * * ** ** *** *** **** **** ********* |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 2 3 4 5 |
1 12 123 1234 12345 |
1 2 3 4 5 6 7 8 9 10 |
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 2 3 4 5 |
1 22 333 4444 55555 |
1 2 3 4 5 6 7 8 9 10 |
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 2 3 4 5 |
1 21 321 4321 54321 |
1 2 3 4 5 6 7 8 9 10 |
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 2 3 4 5 |
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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:
1 2 3 4 5 |
A AB ABC ABCD ABCDE |
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 4 5 |
A BB CCC DDDD EEEEE |
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 4 5 |
A BA CBA DCBA EDCBA |
1 2 3 4 5 6 7 8 9 10 |
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.
4:58 am
I want this pattern, :
a b
aa bb
aaabbb
4:03 am
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( );
}
}
}
9:11 am
Input
N=5
Output :
L
LE
LED
LE
L
How to print this pattern?
1:22 pm
How to print
D
A
B A
C B A
D C B A
2:56 am
How to print-
* *
* *
* *
* *
* *
3:51 am
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( );
}
}
}
12:44 pm
A
B D
E H K
8:19 am
Input: PROGRAM
Output:
G
GR
GRA
GRAM
GRAMP
GRAMPR
GRAMPRO
9:26 am
I want … this pattern
A
AB
ABA
ABAB
ABABA
7:44 am
Can you please solve this:
a
a 1
a 2 a
a 3 a 4
a 5 a 6 a
8:44 am
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();
}
4:22 pm
How to do this program.???
1234321
123 321
12 21
1 1
7:14 am
I want this a-b-c patterns con some one help ,me for this, i need code.
8:50 am
Comment a sample pattern of which u need code of
10:47 am
*
1 2
* * *
1 2 3 4
7:25 am
Send me code
5:27 am
ABC
ABC
ABC
6:57 am
program for the below pattern??
AAAA1
AAA12
AA123
A1234
12345
11:35 am
How to do this program?
Thank you
*** *** ** *** **
** ** ** ** ** ** ** **
** *** ** ** ** ** **
** * ** ** ** ** **
** ** ** ** ** **
** ** **** ****
4:03 am
how to make this pattern??
*
*A*
*A*A*