Hello friends, Now in this blog, you'll learn print the Hollow Rectangle Pattern in Java. In the earlier blog, I have shared Print the Solid Rectangle Pattern in Java.
This program asks the user to input the number of rows and number of columns to print the size of the Hollow Rectangle Pattern.
Now first we can create class name of HollowRectangle and save it with the same name of class with .java extension, after creating the main method, then out of the main method we import java.util.Scanner class and in the main method we create an object of Scanner class sc. Then create two variables for a is the number of Rows and b is the number of Column of Hollow Rectangle Pattern. Then call it for the user inter the input for using sc object and store in variables of a and b. Then we can create 2 nested for loop, outer for loop for rows and inner for loop for columns. Then in the inner for loop we can create conditions for empty space in the rectangle. The if condition is print Stars and else condition is print empty space. And after the inner for loop we can print a new line.
import java.util.Scanner; public class HollowRectangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of Rows: "); int a = sc.nextInt(); System.out.print("Enter the number of Column: "); int b = sc.nextInt(); for (int i = 1; i <= a; i++) { for (int j = 1; j <= b; j++) { if (i == 1 || j == 1 || i == a || j == b) { System.out.print("*"); } else { System.out.print(" "); } } System.out.print("\n"); } } }
After writing a java source file to save with an extension of .java. Then open Command prompt on the file containing the source code of java. After writing and saving Java source code, the ‘javac HollowRectangle.java’ command can be used by the compiler to create a .class file. Once the .class file is created, the ‘java HollowRectangle’ command can be used to run the java program.
You might like this:
- Solid Rectangle Pattern in Java
- Email Subscription Popup Form Design
- Active Tabs Hover Animation with Icons
- Advanced Drop-down Menu Bar Animation
Give numbers of rows and column from user, print a rectangle pattern as shown in the below example.
Example :
Input : row = 5, column = 4
Output :
* * * *
* * * *
* * * *
* * * *
* * * *
//C Program to Print Rectangle Pattern #include<stdio.h> int main() { int row, column; printf("Enter Numbers of Rows : "); scanf("%d", &row); printf("Enter Numbers of Columns : "); scanf("%d", &column); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { printf("* "); } printf("\n"); } return 0; }
//CPP Program to Print Rectangle Pattern #include<iostream> using namespace std; int main() { int row, column; cout << "Enter Numbers of Rows : "; cin >> row; cout << "Enter Numbers of Columns : "; cin >> column; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { cout << "* "; } cout << endl; } return 0; }
//Java Program to Print Rectangle Pattern import java.util.Scanner; public class Rectangle_Pattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int row, column; System.out.print("Enter Numbers of Rows : "); row = sc.nextInt(); System.out.print("Enter Numbers of Columns : "); column = sc.nextInt(); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { System.out.print("* "); } System.out.println(); } } }
#Python Program to Print Rectangle Pattern row = int(input("Enter Numbers of Rows : ")) column = int(input("Enter Numbers of Columns : ")) for i in range(0, row): for j in range(0, column): print("*", end=' ') print()
//CPP Program to Sum of first n natural Numbers #include<iostream> using namespace std; int sum(int n) { int ans = 0; for (int i = 1; i <= n; i++) { ans += i; } return ans; } int main() { int n; cout << "Enter the Value of N : "; cin >> n; cout << sum(n); return 0; }