Square pattern in java

In this article we are going to print square pattern using java code.

Example:- if n = 4

Square pattern in java

Steps:-

  • Take input for row and column from user using scanner
  • Run a loop for rows from i= 1to i<=n.
  • inside first loop run another loop for column from j=1 to j<=n. and print *.
  • After termination of second loop print new line.

Java code to print square pattern

package gangforcode;
import java.util.*;
public class InvertedTriangle {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		int n=sc.nextInt();
		int i=1;
		while(i<=n) {
			int j=1;
			while(j<=(n-i)) {
				System.out.print(" ");
				j++;
				
			}
			while(j<=n) {
				System.out.print("*");
				j++;
			}
			i++;
			System.out.println();
		}
}}

Output:-

Java square pattern

You can print the another programing other programing language like c , c+,+, python etc using the same logic.

Leave a Comment