Java Program to Print Square with Right Rotate N-Numbers by 1 Pattern

Printing Square with Right Rotate N-Numbers by 1 Pattern

In the previous article, we have discussed Java Program to Print Zig-Zag Matrix Number Pattern.

In this article we will see a square pattern with right rotate by 1 number.

Example-1

When size value=5

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
Example-2:

When size value=4

1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach:

  • Enter total size and number store them in integer variables size.
  • Take one outer for loop to iterate the rows,
  • Take 1st inner for loop to print spaces .
  • check that if index numberOfRows is equal to numberOfColumns then print column value .
  • if index numberOfRows is less than numberOfRows then took another 2 inner loop to print the values .
  • After each iteration print a newline.

Java Code to Print Square with Right Rotate N-Numbers by 1 Pattern:

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        // taking size of no. of iteration 
        //Row and column are the iterators
        int size , numberOfRows , numberOfColumns;
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);
        // Get the number of rows from the user
        System.out.println("Enter the number of rows : ");
       size = scanner.nextInt();
        //Outer loop to iterate the rows
        //Iterates from  size to 1  
        for ( numberOfRows = 1 ; numberOfRows <= size ; numberOfRows++) 
        {
            //Inner loop to iterate the columns
         //Iterates from  1   to size
            for (  numberOfColumns = 1 ; numberOfColumns <= size; numberOfColumns++)
               {
                    // check that if index numberOfRows is equal to numberOfColumns
                   if (numberOfRows==numberOfColumns )
                   {
                        System.out.print(numberOfColumns+" ");
                        // if index numberOfRows is less than numberOfRows
                        if (numberOfRows<=numberOfColumns )
                        for(int k=numberOfColumns+1 ; k<=size;k++)
                            System.out.print(k+" "); 
                        for (int p=1;p< numberOfColumns; p++)
                        System.out.print(p+" ");
                   }
               }
            // printing in new line  
            System.out.println();
        }
    }
}
Output :

Enter the number of rows : 5

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

C Code to Print Square with Right Rotate N-Numbers by 1 Pattern:

#include <stdio.h>
#include <stdlib.h>
int main()
{
       int size , numberOfRows , numberOfColumns;
       printf("Enter the number of rows : ");
       scanf ("%d",&size);
       for ( numberOfRows = 1 ; numberOfRows <= size ; numberOfRows++) 
        {
            for (  numberOfColumns = 1 ; numberOfColumns <= size; numberOfColumns++)
               {
                   if (numberOfRows==numberOfColumns )
                   {
                         printf("%d ",numberOfColumns);
                        if (numberOfRows<=numberOfColumns )
                        for(int k=numberOfColumns+1 ; k<=size;k++)
                             printf("%d ",k); 
                        for (int p=1;p< numberOfColumns; p++)
                         printf("%d ",p);
                   }
               }
            printf("\n");
        }
    return 0;
}    

Output :

Enter the number of rows : 5

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

C++ Code to Print Square with Right Rotate N-Numbers by 1 Pattern:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
       int size , numberOfRows , numberOfColumns;
       printf("Enter the number of rows : ");
       scanf ("%d",&size);
        for ( numberOfRows = 1 ; numberOfRows <= size ; numberOfRows++) 
        {
            for (  numberOfColumns = 1 ; numberOfColumns <= size; numberOfColumns++)
               {
                   if (numberOfRows==numberOfColumns )
                   {
                        cout << numberOfColumns ;
                        if (numberOfRows<=numberOfColumns )
                        for(int k=numberOfColumns+1 ; k<=size;k++)
                            cout << k ; 
                        for (int p=1;p< numberOfColumns; p++)
                        cout << p ;
                   }
               }
           cout << "\n";
        }
    return 0;
}
Output :

Enter the number of rows : 5

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

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:

Leave a Comment