Below Java concepts are used to print that patterns
- For Loop
- While Loop
- if..else
1). Program to print Solid Rectangle using Star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i,j;
System.out.println("This is Rectangle program");
Scanner sc= new Scanner(System.in);
System.out.print("enter the number of rows: ");
int rows= sc.nextInt();
Scanner sc1= new Scanner(System.in);
System.out.print("enter the number of columns: ");
int columns= sc1.nextInt();
for(i=1; i<=rows; i++)
{
for(j=1; j<=columns; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Output
This is Rectangle program
enter the number of rows: 3
enter the number of columns: 5
*****
*****
*****
2). Program to print hollow Rectangle using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i,j;
System.out.println("Hollow Reactangle Program");
Scanner sc= new Scanner(System.in);
System.out.print("enter the number of rows: ");
int rows= sc.nextInt();
Scanner sc1= new Scanner(System.in);
System.out.print("enter the number of columns: ");
int columns= sc1.nextInt();
for(i=1; i<=rows; i++)
{
for(j=1; j<=columns; j++)
{
if(i==1 || i==rows || j==1 || j==columns)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println("");
}
}
}
Output
Hollow Reactangle Program
enter the number of rows: 4
enter the number of columns: 8
********
* *
* *
********