Program to Print Square Star Pattern
PRINTING PATTERN:
****
****
****
****
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
- Take number of rows as input from the user (length of side of the square) and store it in any variable (‘l ‘ in this case).
- Run a loop ‘l ‘ number of times to iterate through the rows . From i=0 to i<l. The loop should be structured as for(i=0;i<l;i++) .
- Run a nested loop inside the previous loop to iterate through the columns. From j=0 to j<l. The loop should be structured as for(j=0;j<l;j++) .
- Print ‘*’ inside the nested loop to print ‘*’s in all the columns of a row.
- Move to the next line by printing a new line. printf(“\n”) .
Code in C:
#include<stdio.h> int main() { int i,j,l; //declaring integers i,j for loops and l for number of rows printf("Enter the number of rows/columns\n"); //Asking user for input scanf("%d",&l); //Taking the input for number of rows for(int i=0;i<l;i++) //Outer loop for number of rows { for(int j=0;j<l;j++) //Inner loop for number of columns in each row { printf("*"); //Printing '*' in each column of a row. } printf("\n"); //Printing a new line after each row has been printed. } }
Taking input:
DISPLAYING OUTPUT:
Login/Signup to comment
#include
using namespace std;
int main() {
// declaring i and j here and rows also
int i , j, rows;
cin>>rows;// input the rows from the user
for(i =0; i<rows;i++){
for(j=0;j<rows;j++){
cout <<"*"<<" ";
}
cout<<endl;
}
return 0;
}
Output:-
We are taking 4 rows as a input
* * * *
* * * *
* * * *
* * * *
Code in C++
#include
using namespace std;
int main() {
int rows, i, j;
cout << "Enter the number of rows: " <> rows;
for (i = 0; i < rows; i++) {
for (j = 0; j < rows; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Code in JAVA:
import java.util.*;
public class Star1 {
public static void main(String arg[])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
explain the program with clarity
Its simple charan, we have used 2 loops and for a outer loop the inner loop is traversed to its whole limit.
That is the same thing which we are doing here, for each value of i, j loop will iterate from start to end
and hence print * in a square pattern, because we have set the same upper limit of both the loops.
We hope now you must have got the question