CLASS X- ICSE
ARRAYS (SINGLE DIMENSIONAL ARRAY AND DOUBLE DIENSIONAL ARRAY)
Unsolved Programs on Single and Double Dimensional Arrays:
1. Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array (SDA).
Calculate and display the sum of all even numbers and all odd numbers separately.
import java.util.Scanner;
public class EvenOddSum {
public static void main(String args[]) {
int[] numbers = new int[20];
int evenSum = 0, oddSum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 integers:");
// Input 20 numbers
for (int i = 0; i < 20; i++) {
System.out.print("Number " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
// Check if even or odd and add to respective sum
if (numbers[i] % 2 == 0) {
evenSum += numbers[i];
} else {
oddSum += numbers[i];
}
}
// Display results
System.out.println("\nSum of all even numbers: " + evenSum);
System.out.println("Sum of all odd numbers: " + oddSum);
}
}
2 Write a program in Java to store 20 temperatures in (degree F) in a single dimensional array and display all
the temperatures after converting them into C.
Hint: C/5 = (f-32)/ 9
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String args[]) {
double fahrenheit[] = new double[20];
double celsius[] = new double[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 temperatures in Fahrenheit:");
// Input temperatures and convert to Celsius
for (int i = 0; i < 20; i++) {
System.out.print("Temperature " + (i + 1) + " (°F): ");
fahrenheit[i] = sc.nextDouble();
celsius[i] = (5.0 / 9.0) * (fahrenheit[i] - 32);
}
// Display results
System.out.println("\nFahrenheit to Celsius Conversion:");
for (int i = 0; i < 20; i++) {
System.out.printf("F: %.2f°F => C: %.2f°C\n", fahrenheit[i], celsius[i]);
}
}
}
3. Write a program in Java to store 10 numbers (including positive and negative numbers) in a Single
Dimensional Array (SDA). Display all the negative numbers followed by the positive numbers.
Sample Input:
n[0 ] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
15 21 -32 -41 54 61 71 -19 -44 52
public class SeparateNumbers {
public static void main(String args[]) {
int n[] = {15, 21, -32, -41, 54, 61, 71, -19, -44, 52};
System.out.println("Negative Numbers:");
for (int i = 0; i < n.length; i++) {
if (n[i] < 0) {
System.out.print(n[i] + " ");
}
}
System.out.println("\n\nPositive Numbers:");
for (int i = 0; i < n.length; i++) {
if (n[i] >= 0) {
System.out.print(n[i] + " ");
}
}
}
}
4. Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA). Display the numbers which
are prime.
Sample Input:
n[0 ] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
45 65 77 71 90 67 82 19 31 52
Sample Output: 71, 67, 19, 31
import java.util.Scanner;
public class PrimeNumbersInArray {
public static void main(String args[]) {
int numbers[] = new int[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 integers:");
// Input 20 numbers
for (int i = 0; i < 20; i++) {
System.out.print("Number " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
}
System.out.println("\nPrime numbers in the array:");
for (int i = 0; i < 20; i++) {
int num = numbers[i];
boolean isPrime = true;
if (num <= 1) {
// numbers less than or equal to 1 are not prime numbers
isPrime = false;
} else {
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.print(num + " ");
}
}
}
}
5.Write a program to accept name and total marks of N number of students in two single subscript arrays
name[] and totalmarks[ ].
Calculate and print:
(a) The average of the total marks obtained by N number of students.
average = (sum of total marks of all the students)/N]
(b) Deviation of each student's total marks with the average.
(deviation =total marks of a student - average]
import java.util.Scanner;
public class StudentMarksAnalysis {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = sc.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
// Input names and marks
for (int i = 0; i < n; i++) {
sc.nextLine(); // consume newline
System.out.print("Enter name of student " + (i + 1) + ": ");
name[i] = sc.nextLine();
System.out.print("Enter total marks of " + name[i] + ": ");
totalmarks[i] = sc.nextInt();
}
// Calculate average
int sum = 0;
for (int i = 0; i < n; i++) {
sum += totalmarks[i];
}
double average = (double) sum / n;
// Display results
System.out.println("\nAverage Marks: " + average);
System.out.println("\nStudent-wise Deviation from Average:");
for (int i = 0; i < n; i++) {
double deviation = totalmarks[i] - average;
System.out.println(name[i] + " => Marks: " + totalmarks[i] + ", Deviation: " + deviation);
}
}
}
6. Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in six subjects for 100 students.
(b) Calculate the percentage of marks obtained by each candidate. The maximum marks in each subject is 100.
(c) Calculate the Grade as per the given criteria
Percentage Marks Grade
From 80 to 100 A
From 60 to 79 B
From 40to 59 C
Less than 40 D
import java.util.Scanner;
public class StudentGrades {
public static void main(String args[]) {
final int MAX_STUDENTS = 100;
final int SUBJECTS = 6;
int rollNo[] = new int[MAX_STUDENTS];
String name[] = new String[MAX_STUDENTS];
int marks[][] = new int[MAX_STUDENTS][SUBJECTS];
double percentage[] = new double[MAX_STUDENTS];
char grade[] = new char[MAX_STUDENTS];
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students (max 100): ");
int n = sc.nextInt();
// Input data for each student
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for student " + (i + 1) + ":");
System.out.print("Roll No: ");
rollNo[i] = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Name: ");
name[i] = sc.nextLine();
int total = 0;
for (int j = 0; j < SUBJECTS; j++) {
System.out.print("Enter marks in Subject " + (j + 1) + ": ");
marks[i][j] = sc.nextInt();
total += marks[i][j];
}
// Calculate percentage
percentage[i] = (double) total / (SUBJECTS * 100) * 100;
// Assign grade
if (percentage[i] >= 80) {
grade[i] = 'A';
} else if (percentage[i] >= 60) {
grade[i] = 'B';
} else if (percentage[i] >= 40) {
grade[i] = 'C';
} else {
grade[i] = 'D';
}
}
// Display the results
System.out.println("\nResult Summary:");
System.out.printf("%-10s %-15s %-10s %-10s\n", "Roll No", "Name", "Percent", "Grade");
for (int i = 0; i < n; i++) {
System.out.printf("%-10d %-15s %-10.2f %-10c\n", rollNo[i], name[i], percentage[i], grade[i]);
}
sc.close();
}
}
7. Write a program to a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10
numbers in descending order by using Bubble Sort technique. Finally; print the complete list of integers.
Hint: Class average is the average marks obtained by 40 students in a particular subject.
import java.util.Scanner;
public class PartialBubbleSort {
public static void main(String args[]) {
int numbers[] = new int[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 integers:");
for (int i = 0; i < 20; i++) {
System.out.print("Number " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
}
// Bubble sort first 10 in ascending order
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// Bubble sort next 10 in descending order
for (int i = 10; i < 19; i++) {
for (int j = 10; j < 19 - (i - 10); j++) {
if (numbers[j] < numbers[j + 1]) {
// Swap
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// Display the final list
System.out.println("\nFinal List after Sorting:");
for (int i = 0; i < 20; i++) {
System.out.print(numbers[i] + " ");
}
}
}
8. Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and store all the
even numbers in an array even[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of
both the arrays.
import java.util.Scanner;
public class EvenOddSeparation {
public static void main(String args[]) {
int arr[] = new int[20];
int even[] = new int[20];
int odd[] = new int[20];
int evenCount = 0, oddCount = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 integers:");
for (int i = 0; i < 20; i++) {
System.out.print("Number " + (i + 1) + ": ");
arr[i] = sc.nextInt();
// Store in even or odd array
if (arr[i] % 2 == 0) {
even[evenCount] = arr[i];
evenCount++;
} else {
odd[oddCount] = arr[i];
oddCount++;
}
}
// Display even numbers
System.out.println("\nEven Numbers:");
for (int i = 0; i < evenCount; i++) {
System.out.print(even[i] + " ");
}
// Display odd numbers
System.out.println("\n\nOdd Numbers:");
for (int i = 0; i < oddCount; i++) {
System.out.print(odd[i] + " ");
}
}
}
9. Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those numbers
that are perfect squares.
Sample Input:
n[0 ] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
12 45 49 78 64 77 81 99 45 33
Sample Output: 49, 64, 81
import java.util.Scanner;
public class PerfectSquares {
public static void main(String args[]) {
int arr[] = new int[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 integers:");
// Input 20 numbers
for (int i = 0; i < 20; i++) {
System.out.print("Number " + (i + 1) + ": ");
arr[i] = sc.nextInt();
}
System.out.println("\nPerfect Square Numbers:");
for (int i = 0; i < 20; i++) {
int num = arr[i];
if (num >= 0) {
int sqrt = (int) Math.sqrt(num);
if (sqrt * sqrt == num) {
System.out.print(num + " ");
}
}
}
}
}
10. To get promotion in a Science stream, a student must pass in English and should pass in any of the two
subjects (i.e, Physics, Chemistry or Maths). The passing mark in each subject is 35. Write a program in a Single
Dimensional Array to accept the roll numbers and marks secured in the subjects for all the students. The
program should check and display the roll numbers along with a message whether "Promotion is Granted" or
Promotion is not Granted". Assume that there are 40 students in the class.
import java.util.Scanner;
public class PromotionCheck {
public static void main(String args[]) {
final int STUDENTS = 40;
int roll[] = new int[STUDENTS];
int english[] = new int[STUDENTS];
int physics[] = new int[STUDENTS];
int chemistry[] = new int[STUDENTS];
int maths[] = new int[STUDENTS];
Scanner sc = new Scanner(System.in);
// Input data for each student
for (int i = 0; i < STUDENTS; i++) {
System.out.println("\nEnter details for Student " + (i + 1) + ":");
System.out.print("Roll Number: ");
roll[i] = sc.nextInt();
System.out.print("English Marks: ");
english[i] = sc.nextInt();
System.out.print("Physics Marks: ");
physics[i] = sc.nextInt();
System.out.print("Chemistry Marks: ");
chemistry[i] = sc.nextInt();
System.out.print("Maths Marks: ");
maths[i] = sc.nextInt();
}
// Check and display promotion status
System.out.println("\nPromotion Result:");
for (int i = 0; i < STUDENTS; i++) {
int passCount = 0;
if (physics[i] >= 35) passCount++;
if (chemistry[i] >= 35) passCount++;
if (maths[i] >= 35) passCount++;
if (english[i] >= 35 && passCount >= 2) {
System.out.println("Roll No: " + roll[i] + " - Promotion is Granted");
} else {
System.out.println("Roll No: " + roll[i] + " - Promotion is Not Granted");
}
}
}
}
11. The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array (SDA) is
as follows:
Roll No. Subject A Subject B Subject C
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is above 80.
(c) Print the roll number and the average marks of the students whose average is below 80.
import java.util.Scanner;
public class StudentMarks {
public static void main(String[] args) {
final int STUDENTS = 50;
int[] rollNo = new int[STUDENTS];
int[] subjectA = new int[STUDENTS];
int[] subjectB = new int[STUDENTS];
int[] subjectC = new int[STUDENTS];
double[] average = new double[STUDENTS];
Scanner sc = new Scanner(System.in);
// Input marks and roll numbers
for (int i = 0; i < STUDENTS; i++) {
System.out.println("Enter data for student " + (i + 1));
System.out.print("Roll No: ");
rollNo[i] = sc.nextInt();
System.out.print("Marks in Subject A: ");
subjectA[i] = sc.nextInt();
System.out.print("Marks in Subject B: ");
subjectB[i] = sc.nextInt();
System.out.print("Marks in Subject C: ");
subjectC[i] = sc.nextInt();
// Calculate average
average[i] = (subjectA[i] + subjectB[i] + subjectC[i]) / 3.0;
}
// Display average of each student
System.out.println("\n(a) Average marks of each student:");
for (int i = 0; i < STUDENTS; i++) {
System.out.println("Roll No: " + rollNo[i] + " | Average Marks: " + average[i]);
}
// (b) Students with average > 80
System.out.println("\n(b) Students with average above 80:");
for (int i = 0; i < STUDENTS; i++) {
if (average[i] > 80) {
System.out.println("Roll No: " + rollNo[i] + " | Average Marks: " + average[i]);
}
}
// (c) Students with average < 80
System.out.println("\n(c) Students with average below 80:");
for (int i = 0; i < STUDENTS; i++) {
if (average[i] < 80) {
System.out.println("Roll No: " + rollNo[i] + " | Average Marks: " + average[i]);
}
}
sc.close();
}
}
12. Write a program to store 6 elements in an array P and 4 elements in an array Q. Now, produce a third array
R, containing all the elements of Array P and Q. Display the resultant array.
Input Input Output
P[] Q[] R[]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8
public class MergeArrays {
public static void main(String[] args) {
// Input arrays
int[] P = {4, 6, 1, 2, 3, 10};
int[] Q = {19, 23, 7, 8};
// Create array R to hold all elements from P and Q
int[] R = new int[P.length + Q.length];
// Copy elements of P into R
for (int i = 0; i < P.length; i++) {
R[i] = P[i];
}
// Copy elements of Q into R
for (int i = 0; i < Q.length; i++) {
R[P.length + i] = Q[i];
}
// Display the resulting array R
System.out.println("Resultant array R:");
for (int i = 0; i < R.length; i++) {
System.out.println(R[i]);
}
}
}
13. Write a program to accept the year of graduation from school as an integer for the user. Using the binary
search technique on the sorted array of integers given below, output the message "Record exists" if the value
input is located in the array. If not, output the message Record does not exist"
Sample Input:
n[0 ] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
import java.util.Scanner;
public class GraduationYearSearch {
public static void main(String[] args) {
int[] n = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
Scanner sc = new Scanner(System.in);
System.out.print("Enter year of graduation: ");
int key = sc.nextInt();
int low = 0, high = n.length - 1;
boolean found = false;
// Binary search algorithm
while (low <= high) {
int mid = (low + high) / 2;
if (n[mid] == key) {
found = true;
break;
} else if (key < n[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
// Output the result
if (found) {
System.out.println("Record exists");
} else {
System.out.println("Record does not exist");
}
sc.close();
}
}
14. Write a program to input and store roll numbers, names and marks in 3 subjects of n number of students in
five single dimensional arrays and display the remark based on average marks as given below:
Average Marks Remark
85-100 Excellent
75-84 Distinction
60-74 First Class
40-59 Pass
Less than 40 poor
import java.util.Scanner;
public class StudentRemarks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = sc.nextInt();
sc.nextLine(); // consume newline
int[] rollNo = new int[n];
String[] names = new String[n];
int[] subject1 = new int[n];
int[] subject2 = new int[n];
int[] subject3 = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for student " + (i + 1));
System.out.print("Roll Number: ");
rollNo[i] = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Name: ");
names[i] = sc.nextLine();
System.out.print("Marks in Subject 1: ");
subject1[i] = sc.nextInt();
System.out.print("Marks in Subject 2: ");
subject2[i] = sc.nextInt();
System.out.print("Marks in Subject 3: ");
subject3[i] = sc.nextInt();
}
// Display results
System.out.println("\nResults:");
System.out.printf("%-10s %-15s %-10s %-10s %-10s %-10s %-15s\n",
"Roll No", "Name", "Sub1", "Sub2", "Sub3", "Average", "Remark");
for (int i = 0; i < n; i++) {
double avg = (subject1[i] + subject2[i] + subject3[i]) / 3.0;
String remark;
// Determine remark
if (avg >= 85 && avg <= 100)
remark = "Excellent";
else if (avg >= 75)
remark = "Distinction";
else if (avg >= 60)
remark = "First Class";
else if (avg >= 40)
remark = "Pass";
else
remark = "Poor";
// Print student data
System.out.printf("%-10d %-15s %-10d %-10d %-10d %-10.2f %-15s\n",
rollNo[i], names[i], subject1[i], subject2[i], subject3[i], avg, remark);
}
sc.close();
}
}
Format Specifiers Used:
Each format specifier here is of the form:
%-Ns
Where:
• % → Format specifier begins
• - → Left-justifies the text (so that the text starts from the left of the column)
• N → Width of the field (in number of characters)
• s → Data type is a string (%s for string)
15. A double dimensional array is defined as N[4][4] to store numbers. Write a program to find the sum of all
even numbers and product of all odd numbers of the elements stored in Double Dimensional Array (DDA)
Sample Input.
12 10 15 17
30 11 32 71
17 14 29 31
41 33 40 51
Sample Output:
Product of all odd numbers:……..
Sum of all even numbers:.......
import java.util.Scanner;
public class EvenOddMatrix {
public static void main(String[] args) {
int[][] N = new int[4][4];
int evenSum = 0;
long oddProduct = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 16 numbers (4x4 matrix):");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = sc.nextInt();
}
}
// Process the matrix
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (N[i][j] % 2 == 0) {
evenSum += N[i][j];
} else {
oddProduct *= N[i][j];
}
}
}
// Output results
System.out.println("\nSum of all even numbers: " + evenSum);
System.out.println("Product of all odd numbers: " + oddProduct);
sc.close();
}
}
16. A Departmental Shop has 5 stores and 6 departments. The monthly sale of the department is kept in the
Double Dimensional Array (DDA) as m[5|[6]. The Manager of the shop wants to know the total monthly sale of
each store and each department at any time. Write a program to perform the given task.
Hint: Number of stores as rows and Number of departments as columns.
import java.util.Scanner;
public class DepartmentalSales {
public static void main(String[] args) {
int[][] m = new int[5][6]; // 5 stores × 6 departments
Scanner sc = new Scanner(System.in);
// Input sales data
System.out.println("Enter monthly sales for 5 stores and 6 departments:");
for (int store = 0; store < 5; store++) {
for (int dept = 0; dept < 6; dept++) {
System.out.print("Store " + (store + 1) + ", Department " + (dept + 1) + ": ");
m[store][dept] = sc.nextInt();
}
}
// Total sale of each store (row-wise)
System.out.println("\nTotal Monthly Sale for Each Store:");
for (int store = 0; store < 5; store++) {
int storeTotal = 0;
for (int dept = 0; dept < 6; dept++) {
storeTotal += m[store][dept];
}
System.out.println("Store " + (store + 1) + ": " + storeTotal);
}
// Total sale of each department (column-wise)
System.out.println("\nTotal Monthly Sale for Each Department:");
for (int dept = 0; dept < 6; dept++) {
int deptTotal = 0;
for (int store = 0; store < 5; store++) {
deptTotal += m[store][dept];
}
System.out.println("Department " + (dept + 1) + ": " + deptTotal);
}
sc.close();
}
}
17. A Metropolitan Hotel has 5 floors and 10 rooms in each floor. The names of the visitors are entered in a
Double Dimensional Array (DDA) as M[5][10]. The Hotel Manager wants to know from the 'Enquiry' about the
position of the visitor. (i.e., floor number and room number) as soon as he enters the name of the visitor. Write
a program in Java to perform the above task.
import java.util.Scanner;
public class HotelVisitorSearch {
public static void main(String[] args) {
String[][] M = new String[5][10]; // 5 floors, 10 rooms each
Scanner sc = new Scanner(System.in);
// Input visitor names
System.out.println("Enter names of visitors for 5 floors and 10 rooms:");
for (int floor = 0; floor < 5; floor++) {
for (int room = 0; room < 10; room++) {
System.out.print("Enter name for Floor " + (floor + 1) + ", Room " + (room + 1) + ": ");
M[floor][room] = sc.nextLine();
}
}
// Search for a visitor by name
System.out.print("\nEnter name of visitor to search: ");
String searchName = sc.nextLine();
boolean found = false;
// Search logic
for (int floor = 0; floor < 5; floor++) {
for (int room = 0; room < 10; room++) {
if (M[floor][room].equalsIgnoreCase(searchName)) {
System.out.println("Visitor '" + searchName + "' is in:");
System.out.println("Floor: " + (floor + 1));
System.out.println("Room: " + (room + 1));
found = true;
break; // Stop after first match
}
}
if (found) break;
}
if (!found) {
System.out.println("Visitor '" + searchName + "' not found in the hotel records.");
}
sc.close();
}
}
18. A Class Teacher wants to keep the records of 40 students of her class along with their names and marks
obtained in English, Hindi, Maths, Science and Computer Science in a Double Dimensional Array DDA) as
M[40][5].
When the teacher enters the name of a student as an input, the program must display the name, marks
obtained in the 5 subjects and the total. Write a program in Java to perform the task.
import java.util.Scanner;
public class StudentRecord {
public static void main(String[] args) {
final int STUDENTS = 40;
final int SUBJECTS = 5;
String[] names = new String[STUDENTS];
int[][] M = new int[STUDENTS][SUBJECTS]; // Marks in 5 subjects
Scanner sc = new Scanner(System.in);
// Input student names and marks
for (int i = 0; i < STUDENTS; i++) {
System.out.println("\nEnter details for student " + (i + 1));
System.out.print("Name: ");
names[i] = sc.nextLine();
System.out.print("English marks: ");
M[i][0] = sc.nextInt();
System.out.print("Hindi marks: ");
M[i][1] = sc.nextInt();
System.out.print("Maths marks: ");
M[i][2] = sc.nextInt();
System.out.print("Science marks: ");
M[i][3] = sc.nextInt();
System.out.print("Computer Science marks: ");
M[i][4] = sc.nextInt();
sc.nextLine(); // consume leftover newline
}
// Search for a student's marks
System.out.print("\nEnter the name of the student to search: ");
String searchName = sc.nextLine();
boolean found = false;
for (int i = 0; i < STUDENTS; i++) {
if (names[i].equalsIgnoreCase(searchName)) {
System.out.println("\nStudent Name: " + names[i]);
System.out.println("English: " + M[i][0]);
System.out.println("Hindi: " + M[i][1]);
System.out.println("Maths: " + M[i][2]);
System.out.println("Science: " + M[i][3]);
System.out.println("Computer Science: " + M[i][4]);
int total = M[i][0] + M[i][1] + M[i][2] + M[i][3] + M[i][4];
System.out.println("Total Marks: " + total);
found = true;
break;
}
}
if (!found) {
System.out.println("Student '" + searchName + "' not found.");
}
sc.close();
}
}
19. If arrays M and M + N are as shown below, write a program in Java to find the array N
M=
M+N =
import java.util.Scanner;
public class Find2DArrayN {
public static void main(String[] args) {
final int SIZE = 3;
int[][] M = new int[SIZE][SIZE];
int[][] MN = new int[SIZE][SIZE];
int[][] N = new int[SIZE][SIZE];
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements of 3x3 array M:");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
M[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of 3x3 array M+N:");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
MN[i][j] = sc.nextInt();
}
}
// Calculate N = (M+N) - M
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
N[i][j] = MN[i][j] - M[i][j];
}
}
// Display result
System.out.println("Array N:");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print(N[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}