Rectangle Pattern Program In Java that Most Asked in Interview

  • For Loop
  • While Loop
  • if..else

1). Program to print Solid Rectangle using star(*) in Java

solid rectangle pattern program in C
import java.util.*;  
class Main{
    public static void main(String ...a){
    int i;
    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

2). Program to print hollow Rectangle using star(*) in Java

hollow rectangle program in c
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 rectangle program in c

Leave a Comment