Computer Science
Given a square matrix M[][] of order 'n'. The maximum value possible for 'n' is 10. Accept three different characters from the keyboard and fill the array according to the output shown in the examples. If the value of n exceeds 10 then an appropriate message should be displayed.
Example 1
Enter Size: 4
Input:
First Character '*'
Second Character '?'
Third Character '#'
Output:
# * * #
? # # ?
? # # ?
# * * #
Example 2
Enter Size: 5
Input:
First Character '
Output:
@ @
! @ @ !
@ $ @
Example 3
Enter Size: 65
Output:
Size out of Range
Java
Java Arrays
14 Likes
Answer
import java.util.Scanner;
public class KboatCharMatrix
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter size: ");
int n = in.nextInt();
if (n < 1 || n > 10) {
System.out.println("Size out of Range");
return;
}
System.out.print("First Character: ");
char ch1 = in.next().charAt(0);
System.out.print("Second Character: ");
char ch2 = in.next().charAt(0);
System.out.print("Third Character: ");
char ch3 = in.next().charAt(0);
char m[][] = new char[n][n];
char x = ch2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || (i + j) == n - 1) {
m[i][j] = ch3;
}
else if (i == 0 || i == n - 1) {
m[i][j] = ch1;
}
else if (j == 0 || j == n - 1 || i % 2 == 0) {
m[i][j] = ch2;
}
else {
m[i][j] = ch1;
}
}
}
System.out.println("OUTPUT:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(m[i][j] + "\t");
}
System.out.println();
}
}
}
Output
Answered By
5 Likes
Related Questions
The encryption of letters are to be done as follows:
A = 1
B = 2
C = 3 . . .
Z = 26The potential of a word is found by adding the encrypted value of the letters.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45Accept a sentence which is terminated by either " . " , " ? " or " ! ". Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ~~alphabetical~~ ~~order~~ increasing order of their potential. Output the result in the format given below:
Example 1
Input:
THE SKY IS THE LIMIT.Potential:
THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63Output:
IS THE THE SKY LIMITExample 2
Input:
LOOK BEFORE YOU LEAP.Potential:
LOOK = 53
BEFORE = 51
YOU = 61
LEAP = 34Output:
LEAP BEFORE LOOK YOUA company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the following data and some random data:
Example 1
INPUT:
N = 726OUTPUT:
48 * 15 = 720
6 * 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16Example 2
INPUT:
N = 140OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6 * 1 = 6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6Example 3
INPUT:
N = 4296OUTPUT:
INVALID INPUTWrite a Program in Java to input elements in a 2D square matrix and check whether it is a Lower Triangular Matrix or not.
Lower Triangular Matrix: A Lower Triangular matrix is a square matrix in which all the entries above the main diagonal [] are zero. The entries below or on the main diagonal must be non zero values.
Enter the size of the matrix: 4
The Matrix is:
5 0 0 0
3 1 0 0
4 9 4 0
6 8 7 2The Matrix is Lower Triangular
The result of a quiz competition is to be prepared as follows:
The quiz has five questions with four multiple choices (A, B, C, D), with each question carrying 1 mark for the correct answer. Design a program to accept the number of participants N such that N must be greater than 3 and less than 11. Create a double-dimensional array of size (Nx5) to store the answers of each participant row-wise. Calculate the marks for each participant by matching the correct answer stored in a single-dimensional array of size 5. Display the scores for each participant and also the participant(s) having the highest score.
Example: If the value of N = 4, then the array would be:
Q1 Q2 Q3 Q4 Q5 Participant 1 A B B C A Participant 2 D A D C B Participant 3 A A B A C Participant 4 D C C A B Key to the question: D C C B A Note: Array entries are line fed (i.e. one entry per line)
Test your program for the following data and some random data.
Example 1
INPUT:
N = 5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A AOUTPUT:
Scores:
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Highest Score:
Participant 5Example 2
INPUT:
N = 4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B BOUTPUT:
Scores:
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest Score:
Participant 1
Participant 4Example 3
INPUT:
N = 12OUTPUT:
INPUT SIZE OUT OF RANGE.