Java Program to Create a Matrix and Fill it With Armstrong Number


In Java, a matrix can be represented using a two-dimensional array. Matrices are used for storing and manipulating data that have a tabular structure. Matrices have several properties and operations associated with them, such as addition, subtraction, multiplication, transposition, and determinant calculation.

As per the problem statement we have to Create a Matrix and Fill it with Armstrong Number.

Let's start!

For instance

Suppose we have 4*3 matrix:

After performing the operation on matrix, the result will be:

Enter the number of rows: 4

Enter the number of columns: 3

Armstrong Matrix:

0 1 2 
3 4 5 
6 7 8 
9 153 370 

Algorithm

Step-1: Take the size of the matrix.

Step-2: Create a square matrix.

Step-3: Check for Armstrong number and insert it into the matrix.

Step-4: Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Matrix Elements

  • By Using User Defined Method

Let's see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array Elements

In this approach, matrix elements will be initialized in the program. Then as per the algorithm Create a Matrix and Fill it with Armstrong Number.

Example

import java.util.Scanner;
public class Main 
{
   public static void main(String[] args) 
   {
      // taking size of the matrix
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter the number of rows: ");
      int rows = scanner.nextInt();
      System.out.print("Enter the number of columns: ");
      int columns = scanner.nextInt();
      // create a square matrix
      int[][] matrix = createArmstrongMatrix(rows, columns);
      // print the result
      System.out.println("Armstrong Matrix:");
      displayMatrix(matrix);
   }
   // Function to create a matrix and fill it with Armstrong numbers
   public static int[][] createArmstrongMatrix(int rows, int columns) {
      int[][] matrix = new int[rows][columns];
      int number = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
            while (!isArmstrongNumber(number)) {
               number++;
            }
            matrix[i][j] = number;
            number++;
         }
      }
      return matrix;
   }
   // Function to check if a number is an Armstrong number
   public static boolean isArmstrongNumber(int number) {
      int originalNumber = number;
      int result = 0;
      int digits = String.valueOf(number).length();

      while (number != 0) {
         int remainder = number % 10;
         result += Math.pow(remainder, digits);
         number /= 10;
      }
      return originalNumber == result;
   }
   // Function to display the matrix
   public static void displayMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int element : row) {
            System.out.print(element + " ");
         }
         System.out.println();
      }
   }
}

Output

Enter the number of rows: 4
Enter the number of columns: 3
Armstrong Matrix:
0 1 2 
3 4 5 
6 7 8 
9 153 370

Approach-2: By Using User Defined Method

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm Create a Matrix and Fill it with Armstrong Number.

Example

import java.util.Scanner;
public class Main 
{
   // main method
   public static void main(String[] args) 
   {
      // taking size of the matrix
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter the number of rows: ");
      int rows = scanner.nextInt();
      System.out.print("Enter the number of columns: ");
      int columns = scanner.nextInt();
      // calling user defined method
      func(rows, columns);    
   }
   // user defined method
   public static void func(int rows, int columns)
   {
      // create a square matrix
      int[][] matrix = createArmstrongMatrix(rows, columns);
      // print the result
      System.out.println("Armstrong Matrix:");
      displayMatrix(matrix);
   }
   // Function to create a matrix and fill it with Armstrong numbers
   public static int[][] createArmstrongMatrix(int rows, int columns) {
      int[][] matrix = new int[rows][columns];
      int number = 0;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
            while (!isArmstrongNumber(number)) {
               number++;
            }
            matrix[i][j] = number;
            number++;
         }
      }
      return matrix;
   }
   // Function to check if a number is an Armstrong number
   public static boolean isArmstrongNumber(int number) {
      int originalNumber = number;
      int result = 0;
      int digits = String.valueOf(number).length();
      while (number != 0) {
         int remainder = number % 10;
         result += Math.pow(remainder, digits);
         number /= 10;
      }
      return originalNumber == result;
   }
   // Function to display the matrix
   public static void displayMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int element : row) {
            System.out.print(element + " ");
         }
         System.out.println();
      }
   }
}

Output

Enter the number of rows: 4
Enter the number of columns: 4
Armstrong Matrix:
0 1 2 3 
4 5 6 7 
8 9 153 370 
371 407 1634 8208

In this article, we explored how to create a matrix and fill that with Armstrong number by using Java programming language.

Updated on: 17-Aug-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements