Pattern Problem | Rectangle Pattern


Test Case #1

Enter length:
10
Enter breadth:
4

Output

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

Test Case #2

Enter length:
50
Enter breadth:
10

Output

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

Program

import java.util.*;

package jothibasu.com;

public class RectanglePattern {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int length,breadth;
System.out.println("Enter length:");
length = scan.nextInt();
System.out.println("Enter breadth:");
breadth = scan.nextInt();
//First Line
for (int i = 0 ; i < length ; i++)
System.out.print("*");
//Moving to next line
System.out.println();
/*Execute the loop up to breadth
  Here [breadth-2] given because
  of considering 1st and last line*/
for (int j = 0 ; j < breadth - 2 ; j++) {
//First * of breadth
System.out.print("*");
//Printing the space inside rectangle
for (int k = 0 ; k < length - 2 ; k++)
System.out.print(" ");
//Last * of breadth
System.out.print("*");
//Moving to next line
System.out.println();
}
//Last line
for (int l = 0 ; l < length ; l++)
System.out.print("*");
//Closing the scanner object
scan.close();
}
}

No comments:

Post a Comment