Java Program to Print Rectangle Star Pattern

Program to Print Rectangle Star Pattern

In this article we are going to see how to print Rectangle star pattern.

Example-1

we entered row value as 6
column value as 10
**********
**********
**********
**********
**********
**********
Example-2

we entered row value as 4
column value as 20
********************
********************
********************
********************

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main{

public static void main(String[] args)
{
    int r, c, row, column;
    Scanner sc= new Scanner(System.in);
    // ask user to enter row and column value
    System.out.print("Enter number of rows = ");
    row=sc.nextInt();
    System.out.print("Enter number of columns = ");
    column=sc.nextInt();

    // loop to iterate over rows
    for(r=1; r<=row; r++)
    {
        // loop to iterate over columns and print values
        for(c=1; c<=column; c++)
        {
            // print star symbols
            System.out.print("*");
        }
        
        // Move to the next line/row
        System.out.println("");
    }
}
}
Output:

Enter number of rows = 5
Enter number of columns = 12
************
************
************
************
************

Method-2 : User Input Character

import java.util.Scanner;
public class Main{

public static void main(String[] args)
{
    int r, c, row, column;
    char ch;
    Scanner sc= new Scanner(System.in);
    // ask user to enter row and column value
    System.out.print("Enter number of rows = ");
    row=sc.nextInt();
    System.out.print("Enter number of columns = ");
    column=sc.nextInt();
    System.out.print("Enter any random character = ");
    ch=sc.next().charAt(0);

    // loop to iterate over rows
    for(r=1; r<=row; r++)
    {
        // loop to iterate over columns and print values
        for(c=1; c<=column; c++)
        {
            // print star symbols
            System.out.print(ch);
        }
        
        // Move to the next line/row
        System.out.println("");
    }
}
}
Output:

Enter number of rows = 5
Enter number of columns = 12
@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@

Explanation:

Let’s understand the program with detailed explanation.

Let we have taken row as 5 and column as 12.

Iteration-I

r=1 (passed through first for loop condition) which will execute till r<=row. It will into enter inner loop which iterates from c=1 till c<=column. It will print the star symbols till the inner loop condition satisfies i.e. total 12 stars. Inner loop condition fails, and control come out of inner loop.

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

Iteration-II

r=2 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It will print the star symbols till the inner loop condition satisfies i.e. total 12 stars. Inner loop condition fails, and control come out of inner loop.

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

Iteration-III

r=3 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It will print the star symbols till the inner loop condition satisfies i.e. total 12 stars. Inner loop condition fails, and control come out of inner loop.

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

Iteration-IV

r=4 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It will print the star symbols till the inner loop condition satisfies i.e. total 12 stars. Inner loop condition fails, and control come out of inner loop.

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

Iteration-V

r=5 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It will print the star symbols till the inner loop condition satisfies i.e. total 12 stars. Inner loop condition fails, and control come out of inner loop.

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

Now r=6, so first for loop condition fails i.e. r<=row. At no more for loop will be executed. At last we will see a pattern like this on the output screen.

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

C Code:

#include <stdio.h>

int main()
{
    int r, c, row, column;

    printf("Enter number of rows = ");
    scanf("%d", &row);
    printf("Enter number of columns = ");
    scanf("%d", &column);

    for(r=1; r<=row; r++)
    {
        for(c=1; c<=column; c++)
        {
            printf("*");
        }
        
        printf("\n");
    }

    return 0;
}
Output:

Enter number of rows = 5
Enter number of columns = 12
************
************
************
************
************

C++ Code:

#include <iostream>
using namespace std;
int main()
{
    int r, c, row, column;

    cout<<"Enter number of rows = ";
    cin>>row;
    cout<<"Enter number of columns = ";
    cin>>column;

    for(r=1; r<=row; r++)
    {
        for(c=1; c<=column; c++)
        {
            cout<<"*";
        }
        
        cout<<"\n";
    }

    return 0;
}
Output:

Enter number of rows = 5
Enter number of columns = 12
************
************
************
************
************

Related Java Star Pattern Programs: