Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
954 views

Pattern Programs in Java Number Patterns

The document discusses 13 different patterns that can be printed in Java programs. It provides the code to print number patterns, star patterns, and alphabet patterns. Each pattern code is presented along with its output. The patterns include printing numbers, letters, stars, and variations like increasing/decreasing the numbers or letters in the patterns in different formats.

Uploaded by

Hadraoui Zakaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
954 views

Pattern Programs in Java Number Patterns

The document discusses 13 different patterns that can be printed in Java programs. It provides the code to print number patterns, star patterns, and alphabet patterns. Each pattern code is presented along with its output. The patterns include printing numbers, letters, stars, and variations like increasing/decreasing the numbers or letters in the patterns in different formats.

Uploaded by

Hadraoui Zakaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

40 Pattern Programs in Java – Number, Star, Alphabet

Patterns
javainterviewpoint.com/pattern-programs-in-java

javainterviewpoint

In this article, we will learn to print the different Pattern Programs in Java, it is the
most famous interview question as it tests our logical skills and understanding of flow
control.

Let’s look into the below possible Pattern Programs in Java which includes Number
patterns, Star patterns, and Alphabet patterns.

1/33
Pattern Programs In Java

Pattern 1:

2/33
package com.javainterviewpoint;

public class Pattern1


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 2:

package com.javainterviewpoint;

public class Pattern2


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char)(alphabet + j -1) + " ");
}
System.out.println();
}
}
}

Output

3/33
## Printing the pattern ##
A
A B
A B C
A B C D
A B C D E

Pattern 3:

package com.javainterviewpoint;

public class Pattern3


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
* *
* * *
* * * *
* * * * *

Pattern 4:

package com.javainterviewpoint;

public class Pattern4


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}

4/33
Output

## Printing the pattern ##


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern 5:

package com.javainterviewpoint;

public class Pattern5


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char)(alphabet + i -1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
B B
C C C
D D D D
E E E E E

Pattern 6:

5/33
package com.javainterviewpoint;

public class Pattern6


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}

for (int k = 1; k <= i; k++)


{
System.out.print("* ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
* *
* * *
* * * *
* * * * *

Pattern 7:

package com.javainterviewpoint;

public class Pattern7


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}

6/33
Output

## Printing the pattern ##


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 8:

package com.javainterviewpoint;

public class Pattern8


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


5
4 5
3 4 5
2 3 4 5
1 2 3 4 5

Pattern 9:

7/33
package com.javainterviewpoint;

public class Pattern9


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet + k - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
A B
A B C
A B C D
A B C D E

Pattern 10:

package com.javainterviewpoint;

public class Pattern10


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= 5; k++)
{
System.out.print((char) (alphabet + k - 1) + " ");
}
System.out.println();
}
}
}

8/33
Output

## Printing the pattern ##


E
D E
C D E
B C D E
A B C D E

Pattern 11:

package com.javainterviewpoint;

public class Pattern11


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= (i * 2) - 1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
***
*****
*******
*********

Pattern 12:

9/33
package com.javainterviewpoint;

public class Pattern12


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern 13:

package com.javainterviewpoint;

public class Pattern13


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet + i - 1) + " ");
}
System.out.println();
}
}
}

10/33
Output

## Printing the pattern ##


A
B B
C C C
D D D D
E E E E E

Pattern 14:

package com.javainterviewpoint;

public class Pattern14


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern 15:

11/33
package com.javainterviewpoint;

public class Pattern15


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

Pattern 16:

package com.javainterviewpoint;

public class Pattern16


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

12/33
Pattern 17:

package com.javainterviewpoint;

public class Pattern17


{
public static void main(String[] args)
{
int rows = 5;
int temp = 1;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(temp + " ");
temp++;
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Pattern 18:

package com.javainterviewpoint;

public class Pattern18


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

13/33
## Printing the pattern ##
A B C D E
A B C D
A B C
A B
A

Pattern 19:

package com.javainterviewpoint;

public class Pattern19


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


E D C B A
E D C B
E D C
E D
E

Pattern 20:

14/33
package com.javainterviewpoint;

public class Pattern20


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


E
E D
E D C
E D C B
E D C B A

Pattern 21:

package com.javainterviewpoint;

public class Pattern21


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (alphabet) + " ");
alphabet++;
}
System.out.println();
}
}
}

Output

15/33
## Printing the pattern ##
A
B C
D E F
G H I J
K L M N O

Pattern 22:

package com.javainterviewpoint;

public class Pattern22


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
int temp = 1;
for (int k = 1; k <= i; k++)
{
System.out.print(temp + " ");
temp = temp * (i - k) / (k);
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Pattern 23:

16/33
package com.javainterviewpoint;

public class Pattern23


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
int temp = 1;
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet - 1 + temp) + "
");
temp = temp * (i - k) / (k);
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
A A
A B A
A C C A
A D F D A

Pattern 24:

17/33
package com.javainterviewpoint;

public class Pattern24


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern 25:

18/33
package com.javainterviewpoint;

public class Pattern25


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i - 1; j++)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
A B
A B C
A B C D
A B C D E
A B C D
A B C
A B
A

Pattern 26:

19/33
package com.javainterviewpoint;

public class Pattern26


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("* ");
}
System.out.println();
}
for (int i = 1; i <= rows - 1; i++)
{
for (int j = rows - 1; j >= i; j--)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Pattern 27:

20/33
package com.javainterviewpoint;

public class Pattern27


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}

for (int i = 1; i <= rows - 1; i++)


{
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for (int k = rows - 1; k >= i; k--)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Pattern 28:

21/33
package com.javainterviewpoint;

public class Pattern28


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 29:

22/33
package com.javainterviewpoint;

public class Pattern29


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Pattern 30:

23/33
package com.javainterviewpoint;

public class Pattern30


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A B C D E
A B C D
A B C
A B
A
A B
A B C
A B C D
A B C D E

Pattern 31:

24/33
package com.javainterviewpoint;

public class Pattern31


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print((char) (alphabet + j - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


E D C B A
D C B A
C B A
B A
A
B A
C B A
D C B A
E D C B A

Pattern 32:

25/33
package com.javainterviewpoint;

public class Pattern32


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print("* ");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

Pattern 33:

26/33
package com.javainterviewpoint;

public class Pattern33


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
for (int l = i - 1; l >= 1; l--)
{
System.out.print(l + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Pattern 34:

27/33
package com.javainterviewpoint;

public class Pattern34


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet + k - 1) + " ");
}
for (int l = i - 1; l >= 1; l--)
{
System.out.print((char) (alphabet + l - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
A B A
A B C B A
A B C D C B A
A B C D E D C B A

Pattern 35:

28/33
package com.javainterviewpoint;

public class Pattern35


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print("0 ");
}
System.out.print(i + " ");
for (int k = i; k < rows; k++)
{
System.out.print("0 ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5

Pattern 36:

package com.javainterviewpoint;

public class Pattern36


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(1 + " ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}

29/33
Output

## Printing the pattern ##


1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5

Pattern 37:

package com.javainterviewpoint;

public class Pattern37


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print((char) (alphabet) + " ");
}
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet + i - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A A A A A
A A A B B
A A C C C
A D D D D
E E E E E

Pattern 38:

30/33
package com.javainterviewpoint;

public class Pattern38


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= rows - i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern 39:

31/33
package com.javainterviewpoint;

public class Pattern39


{
public static void main(String[] args)
{
int alphabet = 65;
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print((char) (alphabet + k - 1) + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= rows - i; k++)
{
System.out.print((char) (alphabet + k - 1) + " ");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


A
A B
A B C
A B C D
A B C D E
A B C D
A B C
A B
A

Pattern 40:

32/33
package com.javainterviewpoint;

public class Pattern40


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= (i * 2) - 1; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows - 1; j >= i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= (i * 2) - 1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

## Printing the pattern ##


*
***
*****
*******
*********
*******
*****
***
*

🙂
Do let me know the patterns that need to be added in the comments. Happy Learning !!

33/33

You might also like