This code explain how to print a square based on user input in java programming.



Program Code :

import java.util.Scanner;
public class Square {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.print("Enter the number of star = ");
int n=scn.nextInt();
for (int i = 1; i <=n; i++) {
for (int j = 1; j <=n; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
}



Output :

 Enter the number of star = 10
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * * *


 
Top