Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are:
(1, 8), (2, 4), (4, 2), (8, 1).
Examples:
Input: N = 8
Output: (1, 2) (2, 4) (4, 2) (8, 1)Input: N = 100
Output:
(1, 100) (2, 50) (4, 25) (5, 20) (10, 10) (20, 5) (25, 4) (50, 2) (100, 1)
Approach:
A matrix is said to be of order m x n if it has m rows and n columns. The total number of elements in a matrix is equal to (m*n). So we start from 1 and check one by one if it divides N(the total number of elements). If it divides, it will be one possible order.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to print all possible order void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format cout << i << " " << n / i << endl; } } } // Driver code int main() { int n = 10; printAllOrder(n); return 0; } |
Java
// Java implementation of the above approach class GFG { // Function to print all possible order static void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format System.out.println( i + " " + n / i ); } } } // Driver code public static void main(String []args) { int n = 10; printAllOrder(n); } } // This code is contributed by ihritik |
Python
# Python implementation of the above approach # Function to print all possible order def printAllOrder(n): # total number of elements in a matrix # of order m * n is equal (m*n) # where m is number of rows and n is # number of columns for i in range(1,n+1): # if n is divisible by i then i # and n/i will be the one # possible order of the matrix if (n % i == 0) : # print the given format print( i ,n // i ) # Driver code n = 10printAllOrder(n) # This code is contributed by ihritik |
C#
// C# implementation of the above approach using System; class GFG { // Function to print all possible order static void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format Console.WriteLine( i + " " + n / i ); } } } // Driver code public static void Main() { int n = 10; printAllOrder(n); } } // This code is contributed by ihritik |
PHP
<?php // PHP implementation of the above approach // Function to print all possible order function printAllOrder($n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for ($i = 1; $i <= $n; $i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if ($n % $i == 0) { // print the given format echo $i, " ", ($n / $i), "\n"; } } } // Driver code $n = 10; printAllOrder($n); // This code is contributed by Ryuga ?> |
1 10 2 5 5 2 10 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Different Operations on Matrices
- Count pairs from two sorted matrices with given sum
- Queries on number of Binary sub-matrices of Given size
- Minimum elements to be added so that two matrices can be multiplied
- Count sub-matrices having sum divisible 'k'
- Program to check if two given matrices are identical
- Python List Equality | Program to check if two given matrices are identical
- Program to concatenate two given Matrices of same size
- Check if the given two matrices are mirror images of one another
- Number of square matrices with all 1s
- Program to multiply two matrices
- Program for addition of two matrices
- Program for subtraction of matrices
- Multiplication of two Matrices in Single line using Numpy in Python
- Python program to add two Matrices
- Python program to multiply two matrices
- Kronecker Product of two matrices
- A square matrix as sum of symmetric and skew-symmetric matrices
- Find the intersection of two Matrices
- XOR of XORs of all sub-matrices
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

