JAVA Program for Printing Incrementing Number Square Pattern

Printing Incrementing Number Square Pattern:

In this problem we’re going to code a Java Program for printing increment number star pattern. Take a number input from user as enter row and col and store it in variable named as row and col after that take a for loop start from i=1 to i<=row and then take a j loop start from j=1 to j<=col that will print i after that write line change statement

Numbered Square Pattern

Algorithm:

  1. Take number of rows/columns as input from the user and save it in any variable (‘row’ and ‘col’ in this case).
  2. Run a loop ‘row’ number of times to iterate through the rows. From i=1 to i<=row.  The loop should be structured as for(int i=1 ; i<=row ; i++)
  3. Run a nested loop inside the previous loop to iterate through the columns. From  j=1 to j<=col. The loop should be structured as for (int j = 1; j <= col; j++)
  4. Inside the nested loop print ‘i’ to print the incremented value in each column of a row.
  5. Inside the main loop print a newline to move to the next line after a row System.out.println(); 

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter row and col");
		int row = sc.nextInt();
		int col = sc.nextInt();

		for(int i=1 ; i<=row ; i++) {
			for (int j = 1; j <= col; j++) 
				System.out.print(i);
			System.out.println();
		}
		
	}

}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

3 comments on “JAVA Program for Printing Incrementing Number Square Pattern”


  • Surya

    public static void main(String ar[])
    {
    Scanner obj=new Scanner(System.in);
    System.out.println(“enter the size”);
    int n=obj.nextInt();
    int i,j;

    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    System.out.print(i);
    }
    System.out.println();

    }


  • Sejal

    For taking input as a single digit in Java.
    public class Main {
    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int count = 1;

    for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
    System.out.print(count);
    }
    count++;
    System.out.println();
    }
    }
    }