Computer Science
Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number.
Example:
2 | 3 | 1 | (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80) |
4 | 0 | 5 | (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80) |
1 | 5 | 6 | (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80) |
Perform the following tasks on the matrix:
- Display the original matrix.
- Calculate the decimal equivalent for each row and display as per the format given below.
Test your program for the following data and some random data:
Example 1:
INPUT:
M = 1
N = 3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
FILLED MATRIX | DECIMAL EQUIVALENT | ||
---|---|---|---|
1 | 4 | 4 | 100 |
Example 2:
INPUT:
M = 3
N = 4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
FILLED MATRIX | DECIMAL EQUIVALENT | |||
---|---|---|---|---|
1 | 1 | 3 | 7 | 607 |
2 | 1 | 0 | 6 | 1094 |
0 | 2 | 4 | 5 | 165 |
Example 3:
INPUT:
M = 3
N = 3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M = 4
N = 6
OUTPUT:
OUT OF RANGE
Java
Java Arrays
ICSE Prac 2020
15 Likes
Answer
import java.util.Scanner;
public class OctalMatrix
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = in.nextInt();
if (m <= 0 || m >= 10 || n <= 2 || n >= 6) {
System.out.println("OUT OF RANGE");
return;
}
int a[][] = new int[m][n];
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;
}
}
}
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
for (int i = 0; i < m; i++) {
int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n - j - 1 );
System.out.print(a[i][j] + " ");
}
System.out.print("\t\t" + decNum);
System.out.println();
}
}
}
Output
![BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-1.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-2.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-3.jpg)
![BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE BlueJ output of Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number. Example: Perform the following tasks on the matrix: (a) Display the original matrix. (b) Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M = 1 N = 3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: Example 2: INPUT: M = 3 N = 4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: Example 3: INPUT: M = 3 N = 3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M = 4 N = 6 OUTPUT: OUT OF RANGE](https://cdn1.knowledgeboat.com/img/abp12/1/2020-40-p2-4.jpg)
Answered By
6 Likes
Related Questions
The statement used to find the total number of Strings present in the string array String s[] is:
- s.length
- s.length()
- length(s)
- len(s)
What is the output of the Java code given below?
String color[] = {"Blue", "Red", "Violet"}; System.out.println(color[2].length());
- 6
- 5
- 3
- 2
Write a Java program to store n numbers in an one dimensional array. Pass this array to a function number(int a[]). Display only those numbers whose sum of digit is prime.
Name the below structure:
- One dimensional array
- Two Dimensional array with 4 rows and 5 columns
- Three dimensional array
- Two Dimensional array with 5 rows and 4 columns