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

Square pattern

Programs to print the square pattern are given here. The length of the side of the pattern is taken as input.






1. Program & output to print the square pattern




1.1. C Program & output to print the square pattern

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

#include <stdio.h>

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

    // take input
    printf("Enter the length of the side of square: ");
    scanf("%d", & s);

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

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

    return 0;
}

Output


Enter the number of rows: 6


******

******

******

******

******

******




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

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

#include <iostream>

using namespace std;

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

    // take input
    cout << "Enter the length of the side of square: ";
    cin >> s;

    // new line
    cout << "\n";

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

    return 0;
}

Output


Enter the number of rows: 5


*****

*****

*****

*****

*****




1.3. Java Program & output to print the square pattern

Code has been copied
/*********************************
      alphabetacoder.com
Java program for a square 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 s, i, j;

        // take input 
        System.out.print("Enter the length of the side of square: ");
        s = sc.nextInt();

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

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

Output


Enter the number of rows: 3


***

***

***




1.4. Python Program & output to print the square pattern

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

# take input 
s = int(input("Enter the length of the side of square: "))

# new line
print("")

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

Output


Enter the number of rows: 2


**

**




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

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

using System;

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

            // take input
            Console.Write("Enter the length of the side of square: ");
            s = Convert.ToInt32(Console.ReadLine());

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

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

Output


Enter the number of rows: 4


****

****

****

****