Java Program to Print Square with Increasing Number Pattern

Print Square with Increasing Number Pattern

In the previous article, we have discussed Java Program to Print K shape Decreasing Number Pattern

In this program we are going to see how to print the square with increasing number pattern.

Example-1

When size value=5

 1    2   3    4   5
 6    7   8   9  10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25
Example-2:

When size value=3

 1 2 3
 4 5 6
 7 8 9

Now, let’s see the actual program to print it.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter total size and store it in an integer variable size.
  • Take one outer for loop to iterate the rows,
  • Take one inner for loop to iterate the columns and print the column values.
  • After each iteration print a newline.

Java Code to Print Square with Increasing Number Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        //Taking size as input from user
        System.out.print("Size of square : ");
        Scanner scan = new Scanner(System.in);
        int size = scan.nextInt();

        //Row and column are the iterators
        //Holder holds our value after each iteration
        int row, col, holder = 0;

        //Outer loop to iterate the rows
        //Iterates from 1 to the size entered by the user
        for(row=1;row<=size;row++)
        {
                //Inner loop to iterate the columns
                //Iterates from 1 to the size entered by the user
                for(col = 1; col<=size; col++)
                {
                        //Increments the value in the holder then prints the value
                        System.out.print(++holder+" ");
                }
                //Prints a newline
                System.out.println();
        }
    }
}

Output:

Size of square : 5

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

C Code to Print Square with Increasing Number Pattern

#include <stdio.h>

int main()
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    scanf("%d", &size);

    //Row and column are the iterators
    //Holder holds our value after each iteration
    int row, col, holder = 0;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (row = 1; row <= size; row++)
    {
        //Inner loop to iterate the columns
        // Iterates from 1 to the size entered by the user
        for (col = 1; col <= size; col++)
        {
            //Increments the value in the holder then prints the value
            printf("%d ", ++holder);
        }
        //Prints a newline
        printf("\n");
    }
    return 0;
}
Output:

Size of square : 5

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

C++ Code to Print Square with Increasing Number Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking size as input from user
    cout << "Size of square : ";
    int size;
    cin >> size;

    //Row and column are the iterators
    //Holder holds our value after each iteration
    int row, col, holder = 0;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (row = 1; row <= size; row++)
    {
        //Inner loop to iterate the columns
        // Iterates from 1 to the size entered by the user
        for (col = 1; col <= size; col++)
        {
            //Increments the value in the holder then prints the value
            cout << " " << ++holder;
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}


Output:

Size of square : 5

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: