Java Programming : Rectangle star pattern

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

  1. Take range as input from the user.

  2. Run the outer loop till the given range. Inside the outer loop, we also need to run another loop till the given range only.

  3. Inside the another loop, we need to print the star in each column of a row.

  4. 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

  1. outer loop : - for(i=1;i<=n;i++) -> rows

  2. 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 
        }
    }
}