10 basic pattern printing questions in Java you should know!
You must know basic pattern printing questions like Filled rectangle, Hollow rectangle, Left-sided triangle, Reversed Left-sided triangle, etc.
Question 1 : ( Filled rectangle )
Print The following pattern :
**********
**********
**********
Input :
import java.util.Scanner;
public class p1 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Input C : ");
int c = sc.nextInt();
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i<= r; i++){
for (int j = 1; j<= c; j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output :
Question 2 : ( Hollow rectangle )
Print The following pattern :
**********
* *
* *
**********
Input :
import java.util.Scanner;
public class p2 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Input C : ");
int c = sc.nextInt();
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i<= r; i++){
for (int j = 1; j<= c; j++){
if(i==1 || i==r || j== 1 || j== c){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output :
Question 3 : ( Left-sided triangle )
Print The following pattern :
*
**
***
****
Input :
import java.util.Scanner;
public class p3 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i<= r; i++){
for (int j = 1; j<= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output :
Question 4 : ( Reversed Left-sided triangle )
Print The following pattern :
****
***
**
*
Input :
import java.util.Scanner;
public class p4 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
System.out.println("No1 method");
for (int i = r; i>= 1; i--){
for (int j = 1; j<= i; j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("No2 method");
for (int x = 1; x<= r; x++){
for (int y = 1; y <= (r+1-x); y++){
System.out.print("*");
}
System.out.println();
}
}
}
Output :
Question 5 : ( Pyramid Pattern )
Print The following pattern :
*
***
*****
*******
Input :
import java.util.Scanner;
public class p5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= (r - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i) - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output :
Question 6 :
Print The following pattern :
1234567
2345671
3456712
4567123
5671234
6712345
7123456
Input :
import java.util.Scanner;
public class p6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i<= r ; i++){
for (int j = i; j<= r ; j++){
System.out.print(j);
}
for (int k = 1; k<= i-1; k++ ){
System.out.print(k);
}
System.out.println();
}
}
}
Output :
Question 7 :
Print The following pattern :
123456789
123456789
123456789
123456789
Input :
import java.util.Scanner;
public class p7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
System.out.print("Input C : ");
int c = sc.nextInt();
for (int i = 1; i <= r; i++){
for (int j = 1; j<= c ; j++){
System.out.print(j);
}
System.out.println();
}
}
}
Output :
Question 8 :
Print The following pattern :
1212121212
2121212121
1212121212
Input :
import java.util.Scanner;
public class p8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
System.out.print("Input C : ");
int c = sc.nextInt();
for (int i = 1; i <= r; i++){
for (int j = 1; j<= c ; j++){
if((i+j) % 2 == 0){
System.out.print(1);
}else {
System.out.print(2);
}
}
System.out.println();
}
}
}
Output :
Question 9 :
Print The following pattern :
1
12
123
1234
12345
Input :
import java.util.Scanner;
public class p9 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i<= r; i++){
for (int j = 1; j<= i; j++){
System.out.print(j);
}
System.out.println();
}
}
}
Output :
Question 10 :
Print The following pattern :
1
121
12321
1234321
123454321
Input :
import java.util.Scanner;
public class p10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input R : ");
int r = sc.nextInt();
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= (r - 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 :
ย