Print Rectangle Pattern in C / C++ / Java / Python / C#

Rectangle pattern

Programs to print the rectangle pattern have been given here. The length and the width of the pattern are taken as inputs.






1. Program & output to print the rectangle pattern




1.1. C Program & output to print the rectangle pattern

Code has been copied
/********************************
      	alphabetacoder.com
C program for a rectangle pattern
*********************************/

#include <stdio.h>

int main() {
    // declare variables
    int l, w, i, j;

    // take input
    printf("Enter the length: ");
    scanf("%d", & l);
    printf("Enter the width: ");
    scanf("%d", & w);

    // new line
    printf("\n");

    // display the pattern
    for (i = 1; i <= w; i++) {
        for (j = 1; j <= l; j++) {
            // print *
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Output


Enter the length: 8

Enter the width: 4


********

********

********

********




1.2. C++ Program & output to print the rectangle pattern

Code has been copied
/***********************************
      	alphabetacoder.com
C++ program for a rectangle pattern
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int l, w, i, j;

    // take input
    cout << "Enter the length: ";
    cin >> l;
    cout << "Enter the width: ";
    cin >> w;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= w; i++) {
        for (j = 1; j <= l; j++) {
            // print *
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Output


Enter the length: 8

Enter the width: 6


********

********

********

********

********

********




1.3. Java Program & output to print the rectangle pattern

Code has been copied
/************************************
      	alphabetacoder.com
Java program for a rectangle pattern
*************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int l, w, i, j;

        // take input 
        System.out.print("Enter the length: ");
        l = sc.nextInt();
        System.out.print("Enter the width: ");
        w = sc.nextInt();

        // new line
        System.out.println("");

        // display the pattern
        for (i = 1; i <= w; i++) {
            for (j = 1; j <= l; j++) {
                // print *
                System.out.print("*");
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the length: 2

Enter the width: 10


**

**

**

**

**

**

**

**

**

**




1.4. Python Program & output to print the rectangle pattern

Code has been copied
#***************************************
#           alphabetacoder.com
#Python program for a rectangle pattern
#***************************************

# take input 
l = int(input("Enter the length: "))
w = int(input("Enter the width: "))

# new line
print("")

# display the pattern
for i in range(1, w + 1):
    for j in range(1, l + 1):
        # print *
        print("*", end = "")
    # new line
    print("")

Output


Enter the length: 4

Enter the width: 4


****

****

****

****




1.5. C# Program & output to print the rectangle pattern

Code has been copied
/**********************************
      	alphabetacoder.com
C# program for a rectangle pattern
***********************************/

using System;

namespace Rectangle {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int l, w, i, j;

            // take input
            Console.Write("Enter the length: ");
            l = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the width: ");
            w = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 1; i <= w; i++) {
                for (j = 1; j <= l; j++) {
                    // print *
                    Console.Write("*");
                }
                Console.WriteLine("");
            }
            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the length: 10

Enter the width: 4


**********

**********

**********

**********