Example
Input
5
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Required to Know
Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out. print(), and use of for loop, use of nested for loop, and Use of Data types
Steps
Take range as input from the user.
Run the outer loop till the given range. Inside the outer loop, we also need to run another loop till the given range only.
Inside the another loop, we need to print the star in each column of a row.
In the outer loop which is the main loop also, we need to use the next line statement. So that it moves to the next line.
How the loop should be structured
outer loop : - for(i=1;i<=n;i++) -> rows
Inside the outer loop : - for(j=1;j<=n;j++) -> columns
Program
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, n;
int j;
n = sc.nextInt();
for(i=1;i<=n;i++){ //This is the outer loop
for(j=1;j<=n;j++){ // This is the inner loop
System.out.print("* "); //Print the star by providing a space
}
System.out.println(" "); //this will take you to next line
}
}
}