In this problem, what is the role of print() at the last? and why its affecting the code output if not included?
n = int(input("Enter the number: ")) for i in range(n): for j in range(n): if(i==0 or i==n-1 or j==0 or j==n-1): print("*", end=' ') else: print(" ", end=' ') print()Output: For a number (Say '3')
without print() -
* * * * * * * *with print() -
* * * * * * * *
asked Jul 29 at 10:20
3
When you are printing asterisks or spaces with:
print("*", end=' ')and
print("", end=' ')those print statements are not advancing a line due to the "end=' '" bit. That is why you get either an asterisk or space printing one after another. The line of code:
print()isn't actually printing anything, but it is sending a carriage return and line feed to advance to the next line. So if you have a print statement that does not include the "end=' '" directive, your terminal output will advance to the next line.
halfer
19.6k17 gold badges92 silver badges176 bronze badges
answered Jul 29 at 16:06
NoDakkerNoDakker
2,1311 gold badge8 silver badges9 bronze badges
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*).
Examples:
1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both. Where the outer loop is used for numbers of rows and the inner loop is used for printing all stars in a particular row.
2. Hollow Square : Hollow Square requires a little bit improvisation. Here we should again use two loops, where 1st and last row must have all n-stars and remaining rows have
stars only in 1st and last column.
C++
#include <bits/stdc++.h>
using namespace std;
void hollowSquare(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
cout << "*";
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
cout << "*";
else
cout << " ";
cout << "\n";
}
}
void solidSquare(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows; j++)
cout << "*";
cout << "\n";
}
}
void printPattern(int rows)
{
cout << "\nSolid Square:\n";
solidSquare(rows);
cout << "\nHollow Square:\n";
hollowSquare(rows);
}
int main()
{
int rows = 5;
printPattern (rows);
return 0;
}
Java
class GFG
{
static void hollowSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++)
System.out.print("*");
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows)
System.out.print("*");
else
System.out.print(" ");
System.out.print("\n");
}
}
static void solidSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
System.out.print("*");
System.out.print("\n");
}
}
static void printPattern(int rows)
{
System.out.print("\nSolid Square:\n");
solidSquare(rows);
System.out.print("\nHollow Square:\n");
hollowSquare(rows);
}
public static void main (String[] args)
{
int rows = 5;
printPattern (rows);
}
}
Python3
def hollowSquare(rows):
for i in range(1, rows + 1):
if (i == 1 or i == rows):
for j in range(1, rows + 1):
print("*", end = "")
else:
for j in range(1, rows + 1):
if (j == 1 or j == rows):
print("*", end = "")
else:
print(end = " ")
print()
def solidSquare(rows):
for i in range(1, rows):
for j in range(1, rows + 1):
print("*", end = "")
print()
def printPattern(rows):
print("Solid Square:")
solidSquare(rows)
print("\nHollow Square:")
hollowSquare(rows)
rows = 5
printPattern (rows)
C#
using System;
class GFG
{
static void hollowSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++)
Console.Write("*");
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows)
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
}
}
static void solidSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
Console.Write("*");
Console.WriteLine();
}
}
static void printPattern(int rows)
{
Console.Write("\nSolid Square:\n");
solidSquare(rows);
Console.Write("\nHollow Square:\n");
hollowSquare(rows);
}
public static void Main ()
{
int rows = 5;
printPattern (rows);
}
}
PHP
<?php
function hollowSquare($rows)
{
for ($i = 1; $i <= $rows; $i++)
{
if ($i == 1 || $i == $rows)
for ($j = 1; $j <= $rows; $j++)
echo "*";
else
for ($j = 1; $j <= $rows; $j++)
if ($j == 1 || $j == $rows)
echo "*";
else
echo " ";
echo "\n";
}
}
function solidSquare($rows)
{
for ($i = 1; $i <= $rows; $i++)
{
for ($j = 1; $j <= $rows; $j++)
echo "*";
echo "\n";
}
}
function printPattern($rows)
{
echo "\nSolid Square:\n";
solidSquare($rows);
echo "\nHollow Square:\n";
hollowSquare($rows);
}
$rows = 5;
printPattern ($rows);
?>
Javascript
<script>
function hollowSquare(rows) {
var i, j;
for (i = 1; i <= rows; i++) {
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++) document.write("*");
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows) document.write("*");
else document.write(" ");
document.write("<br>");
}
}
function solidSquare(rows) {
var i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++) document.write("*");
document.write("<br>");
}
}
function printPattern(rows) {
document.write("Solid Square:<br>");
solidSquare(rows);
document.write("<br>Hollow Square:<br>");
hollowSquare(rows);
}
var rows = 5;
printPattern(rows);
</script>
Output :
Time Complexity: O(n2), where n represents the
given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article
to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.