Java Program to Print Rectangular with User Input Centre Number Pattern

Print Rectangular with User Input Centre Number Pattern

In the previous article, we have discussed Java Program to Print Heart Number Pattern. In this article, we will see how to print rectangular with User Input Centre number pattern.

When size is 5

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach :

  • Take a variable “size” for dimension for the matrix.
  • Declare the center coordinate of the matrix .
  • Iterate an outer loop by using a variable numberOfRows  and do the following steps:
    • Iterate an inner loop using a variable numberOfColumns  and do the following steps:
    • Print maximum of abs(c1 – i) and abs(c2 – j).

Java Code to Print Rectangular with User Input Centre Number Pattern

import java.io.*;
class Main
{
    public static void main(String[] args)
    {
        //taking the coordinate of the  center of matrix as c1 and c2 
        // taking the dimension of the square of the matrix as size 
        int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns ;
        // // Iterate in the range[0, n-1] for outer loop ( rows)
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                // Iterate in the range[0, n-1] for inner loop (column)
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                        //  maximum of abs(c1 – i) and abs(c2 – j).
                        System.out.print((Math.max(Math.abs(c1 - numberOfRows), Math.abs(c2 - numberOfColumns ))) + " ");
                System.out.println();
            }
    }
}
Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

C Code to Print Rectangular with User Input Centre Number Pattern

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns , result1 , result2 ;
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                    {
                        result1= abs(c1 - numberOfRows);
                        result2=abs(c2 - numberOfColumns);
                        if (result1 > result2)
                            printf( "%d ",result1);
                        else
                           printf( "%d ",result2); 
                    }
                printf("\n");
            }
    return 0;
}

Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

C++ Code to Print Rectangular with User Input Centre Number Pattern

#include <bits/stdc++.h>
using namespace std;
int main()
{
     int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns ;
        // // Iterate in the range[0, n-1] for outer loop ( rows)
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                       cout << max(abs(c1 - numberOfRows), abs(c2 - numberOfColumns)) << " ";
               cout << endl;
            }
    return 0;
}
Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

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: