Java Question for absolute beginner
(Part 3 - Pattern Problems)
INDEX
INDEX 1
1. Pattern 1 - Stars in same line 2
2. Pattern 2 - n x n star rectangle 3
3. Pattern 3 - n x m star rectangle 4
4. Pattern 4 - Right-angled Triangle Pattern 5
5. Pattern 5 - Inverted Right-angled Triangle Pattern 5
6. Pattern 6 - Mirror Right-angled Triangle of * 6
7. Pattern 7 - Right-angled triangle of number from 1 to n 6
8. Pattern 8 - Mirror Right-angled Triangle of whole numbers 7
9. Pattern 9 - Inverted Right-angled Triangle of whole numbers 7
10. Pattern 10 - Right triangle of 5 multiples 8
11. Pattern 11 - Hollow m * n star rectangle. 9
12. Pattern 12 - Hollow square without top 10
13. Pattern 13 - Square Ladder with top and bottom 10
14. Pattern 14 - Pyramid 11
15. Pattern 15 - Diamond 12
16. Pattern 16 - Inverted Pyramid 13
17. Pattern 17 - Big Ladder 14
18. Pattern 18 - V Pattern 15
19. Pattern 19 - Dumroo Pattern 16
20. Pattern 20 - Number Pattern 2 17
21. Pattern 21 - **12 18
22. Pattern 22 - **21 19
Questions are gathered from various site
Linkedin | Git-Hub Document created by - Shirish Jaiswal
2
Pattern 1 - Stars in same line
Take an integer input n and print n stars in the same straight line.
Input Format
You will be given an integer input n.
Output Format
Print as given in the problem statement.
For better understanding, check the test case below.
Sample Input 0
Sample Output 0
*****
Sample Input 1
Sample Output 1
*******
Sample Input 2
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
3
Pattern 2 - n x n star rectangle
Take n as an integer input and then print a star rectangle such that each line has n stars.
Also, there are 12 such lines.
Input Format
For each test case, take n as an integer input.
Output Format
Print according to the problem statement.
For reference, check out the sample test case below.
Sample Input
Sample Output
***
***
***
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
4
Pattern 3 - n x m star rectangle
Take n as an integer input. Then print the n x m star rectangle as mentioned below.
In each line, n stars should be printed.
And there should be n such lines.
Input Format
For each test case, n will be given as an integer input.
Output Format
Print according to the problem statement.
For reference, look at the test cases below.
Sample Input 0
4
4
Sample Output 0
****
****
****
****
Sample Input 1
6
5
Sample Output 1
******
******
******
******
******
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
5
Pattern 4 - Right-angled Triangle Pattern
Take Integer N as input and print the following pattern.
Input Format
Single Line Input Integer Value
Output Format
N Line of Pattern as shown in the problem statement.
Sample Input
Sample Output
*
**
***
****
*****
Pattern 5 - Inverted Right-angled Triangle Pattern
Take an Integer input n and Print the pattern given below.
Input Format
A single line n takes input from the user.
Output Format
Print the pattern .
Sample Input Sample Output
5 *****
****
***
**
*
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
6
Pattern 6 - Mirror Right-angled Triangle of *
Take Integer N as input and print the following pattern.
Input Format
Single Line Input Integer Value
Output Format
N Line of Pattern as shown in the problem statement.
Note: No space in between stars.
Sample Input 0
Sample Output 0
*
**
***
****
*****
Pattern 7 - Right-angled triangle of number from 1 to n
Take Integer N as input and print the following pattern.
Input Format
Single Line Input Integer Value
Output Format
N Line of Pattern as shown in the problem statement.
Sample Input Sample Output
5 1
12
123
1234
12345
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
7
Pattern 8 - Mirror Right-angled Triangle of whole numbers
Take an integer input n and Print the pattern.
Input Format
A single line n takes input from the user.
Output Format
Print the pattern.
Sample Input Sample Output
5 1
12
123
1234
12345
Pattern 9 - Inverted Right-angled Triangle of whole numbers
Take an integer input n and Print the pattern.
Input Format
A single line n takes input from the user.
Output Format
Print the pattern.
Sample Input
Sample Output
1234567
123456
12345
1234
123
12
1
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
8
Pattern 10 - Right triangle of 5 multiples
ake an integer n as an integer input, and then
print the first multiple of 5 in the first line,
print the first two multiples of 5 in the second line.
print first n multiples of 5 in the nth line.
Note: In each line the multiples of 5 should be tab separated.
Input Format
For each test case, take n as an integer input.
Output Format
Print as given in the problem statement.
Sample Input
Sample Output
5
5 10
5 10 15
5 10 15 20
5 10 15 20 25
5 10 15 20 25 30
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
9
Pattern 11 - Hollow m * n star rectangle.
Take m and n as an integer input, then print a hollow m by n star rectangle.
Then print a hollow star rectangle which has m stars in the first line and m stars in the nth line.
Their rectangle should have n lines and every line in between should have only the first star and
then the mth star.
Input Format
For each test case, m will be given in the first line. And n will be given in the second line.
Output Format
Print as given in the test case below.
Sample Input 0
5
4
Sample Output 0
*****
* *
* *
*****
Sample Input 1
7
8
Sample Output 1
*******
* *
* *
* *
* *
* *
* *
*******
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
10
Pattern 12 - Hollow square without top
Take an integer input n and then print a hollow n by n square without the top.
Print as given in the conditions below:
In the first line there will a star , followed by n-2 spaces and then there will be a star again,
Just like above, there will be n-1 lines and then in the last line there will be n stars.
Input Format
For each test case, n will be given as an integer input.
Output Format
Take a look at the test case below for better understanding.
Sample Input
Sample Output
* *
* *
* *
* *
*****
Pattern 13 - Square Ladder with top and bottom
Take n as an integer input, then print n tab separated stars in the first line, then in the second line
print a star, then n-2 tabs, then print a star. then print n tab separated stars in the third line. then in
the fourth line print a star, then n-2 tabs, then print a star .
Continue the pattern like this.
Example: n=5
Pattern will be:
Input Format
For each test case, n will be given as an integer input.
Output Format
Print as given in the problem statement.
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
11
Pattern 14 - Pyramid
Take Integer N as input and print the following pattern.
Input Format
Single Line Input Integer Value
Output Format
N Line of Pattern as shown in the problem statement.
Sample Input
Sample Output
*
**
***
****
*****
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
12
Pattern 15 - Diamond
Take Integer N as input and print the following pattern.
Input Format
Single Line Input Integer Value
Output Format
((2*N)-1) Line of Pattern as shown in problem statement.
Note: No space in between stars.
Sample Input
Sample Output
*
***
*****
*******
*********
*******
*****
***
*
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
13
Pattern 16 - Inverted Pyramid
Print the below pattern.
Take m as an integer input, where m tab separated stars are printed in the first line. Then you have
to print the pattern below.
Eg. for m=7, the output is written under.
Input Format
m will be given as an integer output.
Output Format
Print the pattern as given under
Sample Input 0
Sample Output 0
* * * * *
* * *
*
Sample Input 1
Sample Output 1
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
14
Pattern 17 - Big Ladder
Print the below pattern below
Given m and n integer input, where m tab separated stars are
printed in the first line, and in total there are n lines.
Eg. for m=7,n=13 the output is written under.
Recognise the pattern and print the final output.
Input Format
m will be given in the first line,
n will be given in the second line.
Output Format
Print the pattern accordingly.
Sample Input sample Output Sample Input sample Output
3 Sample Output 0 5 * * * * *
10 13 * *
* * * * *
* * * * * * *
* * * *
* * * * *
* * * * * * *
* * * *
* * * * *
* * * * * * *
* * * *
* * * * *
* * * * *
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
15
Pattern 18 - V Pattern
Print the below pattern using nested for and while loop
Note that : between two characters there is a tab.
Eg. for m=7, the output is written under. Recognise the pattern and print the final output.
Input Format
m will be given as an integer input
Output Format
Print the pattern.
Sample Input 0
7
Sample Output 0
* *
* *
* *
*
Sample Input 1
5
Sample Output 1
* *
* *
*
Sample Input 2
9
Sample Output 2
* *
* *
* *
* *
*
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
16
Pattern 19 - Dumroo Pattern
Print the below pattern Given m as integer input, where m tab separated stars are printed in the
first line, Eg. for m=7, the output is written under. Recognise the pattern and print the final output.
Input Format
m will be given as an integer input in the first line.
Output Format
Print the pattern accordingly.
Sample Input
Sample Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
17
Pattern 20 - Number Pattern 2
Take an Integer input n and k and Print the Pattern below.
K here Denotes the multiples of k.
Input Format
First line n take input from user.
Second line k takes input from the user.
Output Format
Print the series
Sample Input
6
7
Sample Output
7
7 14
7 14 21
7 14 21 28
7 14 21 28 35
7 14 21 28 35 42
Explanation
n=6 and k=7
k here denotes the multiples of k.
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
18
Pattern 21 - **12
Take an Integer input n and write a program as shown for n = 5 below
****1
***12
**123
*1234
12345
Input Format
Take n as input from the user.
Output Format
Print the series
Sample Input
Sample Output
*****1
****12
***123
**1234
*12345
123456
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal
19
Pattern 22 - **21
Take an Integer input n and write a program as shown for n = 5 below
****1
***21
**321
*4321
54321
Input Format
Take n as input from the user.
Output Format
Print the series
Sample Input
Sample Output
*****1
****21
***321
**4321
*54321
654321
Return to index
Linkedin | Git-Hub Document created by - Shirish Jaiswal