Java Program to Print Square with Row wise Increasing Number Pattern

Program to Print Square with Row wise Increasing Number Pattern

In the previous article, we have discussed Java Program to Print Square with Increasing Number Pattern

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

Example-1

When size value=5

 1 2 3 4 5
 2 3 4 5 6
 3 4 5 6 7
 4 5 6 7 8
 5 6 7 8 9
Example-2:

When size value=3

1 2 3
2 3 4
3 4 5

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

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

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.
  • After each iteration print a newline.

Java Code to Print Square with Row wise 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
        int row, col;

        //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 0 to one less than the size entered by the user
                for (col = 0; col < size; col++)
                {
                        //Prints the sum of row and col
                        System.out.print((row + col)+" ");
                }
                //Prints a newline
                System.out.println();
        }
}
}

Output

Size of square : 3

1 2 3
2 3 4
3 4 5

C Code to Print Square with Row wise 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
    int row, col;

    //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 0 to one less than the size entered by the user
        for (col = 0; col < size; col++)
        {
            //Prints the sum of row and col
            printf("%d ", row + col);
        }
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output

Size of square : 3

1 2 3
2 3 4
3 4 5

C++ Code to Print Square with Row wise 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
    int row, col;

    //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 0 to one less than the size entered by the user
        for (col = 0; col < size; col++)
        {
            //Prints the sum of row and col
            cout << " " << row + col;
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}


Output

Size of square : 3

1 2 3
2 3 4
3 4 5

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: