Solid Rectangle Pattern | Write a program in Java to print a solid rectangle | ISC Computer Science | ICSE Computer Application.
Algorithm
- Start
- rows:=0
- columns:=0
- Print "Enter the number of rows: "
- Read rows
- Print "Enter the number of columns: "
- Read columns
- n=rows
- m=columns
- i:=0
- j:=0
- i=1
- If i<=n, go to step 14 or else go to step 20
- j=1
- If j<=m, go to step 16 or else go to step 18
- Print "*"
- j++. Go to step 15
- Go to next line
- i++. Go to step 13
- Stop
Program
Buffered Reader
import java.io.*;
class SolidRectangle
{
int rows, columns;
void accept() throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter the number of rows : ");
rows = Integer.parseInt(br.readLne());
System.out.println("Enter the number of columns : ");
columns = Integer.parseInt(br.readLine());
}
void display(int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
System.out.print("*");
}
System.out.println();
}
}
void main () throws IOException
{
accept();
System.out.println();
display(rows, columns);
}
}
Scanner
import java.util.*;
class SolidRectangle
{
int rows, columns;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows : ");
rows = sc.nextInt();
System.out.println ("Enter the number of columns : ");
columns = sc.nextInt();
}
void display(int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
System.out.print("*");
}
System.out.println();
}
}
void main ()
{
accept();
System.out.println();
display(rows, columns);
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | rows | To store the number of rows |
int | columns | To store the number of columns |
int | m | To parse the value of rows to a function |
int | n | To parse the value of columns to a function |
int | i | To act as a counter variable |
int | j | To act as a counter variable |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user. |
void | display(int, int) | To display the solid rectangle |
void | main() | To act as an entry gateway for the compiler. |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment