Java Program to Print a Square Pattern for given integer Last Updated : 05 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Write a java program to print the given Square Pattern on taking an integer as input from commandline. Examples: Input : 3 Output :1 2 3. 7 8 9 4 5 6 Input :4 Output :1 2 3 4 9 10 11 12 13 14 15 16 5 6 7 8 Java // Java program to print a square pattern with command // line one argument import java.util.*; import java.lang.*; public class SquarePattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int number = sc.nextInt(); int arr[][] = PrintSquarePattern(number); // int num = 3; int k = 0, m = number - 1, n = number; int l = 0; if (number % 2 == 0) m = number - 1; else m = number; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n; j++) { System.out.format("%3d", arr[k][j]); } System.out.println(""); l = l + 2; k = l; // System.out.println(""); } k = number - 1; for (int i = n / 2; i < n; i++) { for (int j = 0; j < n; j++) { System.out.format("%3d", arr[k][j]); } m = m - 2; k = m; System.out.println(""); } } public static int[][] PrintSquarePattern(int n) { int arr[][] = new int[n][n]; int k = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arr[i][j] = k; k++; } } return arr; } } Input : 5 Output : Enter a number 1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 16 17 18 19 20 6 7 8 9 10 Time complexity: O(n^2) for given input n Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Print a Square Pattern for given integer N nikhi2000 Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Program for Number of elements with odd factors in given range Given a range [n, m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output : 3 min read Java Program for Sum of squares of first n natural numbers Given a positive integer N. The task is to find 12 + 22 + 32 + ..... + N2.Examples: Input : N = 4 Output : 30 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30 Input : N = 5 Output : 55 Method 1: O(N) The idea is to run a loop from 1 to n and for each i, 1 <= i <= n, find i2 to sum. Java // Java Program 5 min read Inner reducing pattern printing Given a number N, print the following pattern. Examples : Input : 4 Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Explanation: (1) Given value of n forms the outer-most rectangular box layer. (2) Value of n reduces by 1 and forms an inner rectangular box layer. (3) The step (2) is 5 min read Hollow Half Pyramid Pattern Using Numbers A hollow half-pyramid pattern using numbers is a type of pattern that seems like a pyramid shape mostly it is considered a star pattern but here we will be creating using numbers. Hollow half-pyramid pattern using numbersProgram to print the following pattern of a half pyramid for N. Example: Inpu 8 min read Java Program to Print Square Star Pattern Here, we will implement a Java program to print the square star pattern. We will print the square star pattern with diagonals and without diagonals. Example: ********************** * * * * * * * * * * * * ********************** Approach: Step 1: Input number of rows and columns. Step 2: For rows of 4 min read Java Program to Print Diamond Shape Star Pattern In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of 6 min read Programs for printing pyramid patterns in Java This article is aimed at giving a Java implementation for pattern printing. Simple pyramid pattern Java import java.io.*; // Java code to demonstrate star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop 11 min read Java Program to Print Pyramid Star Pattern This article will guide you through the process of printing a Pyramid star pattern in Java. 1. Simple pyramid pattern Java import java.io.*; // Java code to demonstrate Pyramid star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void PyramidStar(int n 5 min read Java Pattern Programs - Learn How to Print Pattern in Java In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts (especially for loop) and problem-solving skills in Java. If you are looking for a place t 15+ min read Java Program to Print Mirror Lower Star Triangle Pattern In this article, we are going to learn how to print mirror lower star triangle patterns in Java. Illustration: Input: number = 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Methods: We can print mirror lower star triangle pa 5 min read Like