0% found this document useful (0 votes)
4 views15 pages

Solutions To Unsolved Java Programs On SDA Print

The document provides solutions to various Java programming questions involving Single Dimensional Arrays (SDA). It includes programs for tasks such as calculating sums of even and odd numbers, converting temperatures from Fahrenheit to Celsius, sorting numbers, and determining student grades based on marks. Each question is accompanied by example code and expected outputs.

Uploaded by

kongk7395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Solutions To Unsolved Java Programs On SDA Print

The document provides solutions to various Java programming questions involving Single Dimensional Arrays (SDA). It includes programs for tasks such as calculating sums of even and odd numbers, converting temperatures from Fahrenheit to Celsius, sorting numbers, and determining student grades based on marks. Each question is accompanied by example code and expected outputs.

Uploaded by

kongk7395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

Solutions to Unsolved Java Programs on SDA & DDA


Question 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 KboatSDAOddEvenSum


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

int oddSum = 0, evenSum = 0;

for (int i = 0; i < arr.length; i++) {


if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}

System.out.println("Sum of Odd numbers = " + oddSum);


System.out.println("Sum of Even numbers = " + evenSum);
}
}

Question 2
Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array (SDA) and display all
the temperatures after converting them into °C.
Hint: (c/5) = (f - 32) / 9
import java.util.Scanner;

public class KboatSDAF2C


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];

System.out.println("Enter 20 temperatures in degree Fahrenheit");


for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextDouble();
}

System.out.println("Temperatures in degree Celsius");


for (int i = 0; i < arr.length; i++) {
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}

Question 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 without
2

changing the order of the 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

Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;

public class KboatSDANumbers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];

System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

for (int i = 0; i < arr.length; i++) {


if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}

for (int i = 0; i < arr.length; i++) {


if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}

Question 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[16 n[17 n[18 n[19
] ] ] ] ] ] . ] ] ] ]

..
45 65 77 71 90 67 82 19 31 52
.

Sample Output: 71, 67, 19, 31


import java.util.Scanner;

public class KboatSDAPrimeNumbers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {

int c = 0;
3

for (int j = 1; j <= arr[i]; j++) {


if (arr[i] % j == 0) {
c++;
}
}

if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}

Question 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 KboatSDAMarks


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

String name[] = new String[n];


int totalmarks[] = new int[n];
int grandTotal = 0;

for (int i = 0; i < n; i++) {


in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

for (int i = 0; i < n; i++) {


System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}

Question 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 are
100.
(c) Calculate the Grade as per the given criteria:

Percentage Marks Grade

From 80 to 100 A
4

Percentage Marks Grade

From 60 to 79 B

From 40 to 59 C

Less than 40 D

import java.util.Scanner;

public class KboatSDAGrades


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


String name[] = new String[TOTAL_STUDENTS];
int s1[] = new int[TOTAL_STUDENTS];
int s2[] = new int[TOTAL_STUDENTS];
int s3[] = new int[TOTAL_STUDENTS];
int s4[] = new int[TOTAL_STUDENTS];
int s5[] = new int[TOTAL_STUDENTS];
int s6[] = new int[TOTAL_STUDENTS];
double p[] = new double[TOTAL_STUDENTS];
char g[] = new char[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {

System.out.println("Enter student " + (i+1) + " details:");


System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();

p[i] = (((s1[i] + s2[i] + s3[i] + s4[i]


+ s5[i] + s6[i]) / 600.0) * 100);

if (p[i] < 40)


g[i] = 'D';
else if (p[i] < 60)
g[i] = 'C';
else if (p[i] < 80)
g[i] = 'B';
else
g[i] = 'A';
}

System.out.println();
5

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);
}
}
}

Question 7
Write a program to accept 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.
import java.util.Scanner;

public class KboatSDABubbleSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");

for (int i = 0; i < arr.length; i++) {


arr[i] = in.nextInt();
}

//Sort first half in ascending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output

Question 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;
6

public class KboatSDAEvenOdd


{
public static void main(String args[]) {

final int NUM_COUNT = 20;


Scanner in = new Scanner(System.in);
int i = 0;

int arr[] = new int[NUM_COUNT];


int even[] = new int[NUM_COUNT];
int odd[] = new int[NUM_COUNT];

System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}

int eIdx = 0, oIdx = 0;


for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}

System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}

System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}

Output

Question 9
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only those numbers
that are perfect squares.

n[0 n[1 n[2 n[3 n[4 n[5 .. n[16 n[17 n[18 n[19
] ] ] ] ] ] . ] ] ] ]

..
12 45 49 78 64 77 81 99 45 33
.

Sample Output: 49, 64, 81


import java.util.Scanner;

public class KboatSDASquares


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Perfect Squares are:");


7

for (int i = 0; i < arr.length; i++) {


double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}

Output

Question 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 Dimension 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 KboatStudent


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int studentDetails[] = new int[200]; //40 * 5 = 200

System.out.println("Enter student details");


for (int i = 0, idx = 1; i < 200; i = i + 5, idx++) {
System.out.print("Student " + idx + " roll number: ");
studentDetails[i] = in.nextInt();
System.out.print("Student " + idx + " English Marks: ");
studentDetails[i+1] = in.nextInt();
System.out.print("Student " + idx + " Maths Marks: ");
studentDetails[i+2] = in.nextInt();
System.out.print("Student " + idx + " Physics Marks: ");
studentDetails[i+3] = in.nextInt();
System.out.print("Student " + idx + " Chemistry Marks: ");
studentDetails[i+4] = in.nextInt();
}

for (int i = 0; i < 200; i = i + 5) {


System.out.println("Roll No: " + studentDetails[i]);
if (studentDetails[i+1] > 34 &&
((studentDetails[i+2] > 34 && studentDetails[i+3] > 34) ||
(studentDetails[i+2] > 34 && studentDetails[i+4] > 34) ||
(studentDetails[i+3] > 34 && studentDetails[i+4] > 34))) {
System.out.println("Promotion is granted.");
}
else {
System.out.println("Promotion is not granted.");
}
}
}
}

Output

Question 11
The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array (SDA) is as
follows:
8

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 40.
import java.util.Scanner;

public class KboatExamResult


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}

System.out.println("\nRoll No\tAverage Marks");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average above 80:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}
}

Output

Question 12
9

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

import java.util.Scanner;

public class Kboat3Arrays


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;

System.out.println("Enter 6 elements of array P:");


for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}
10

System.out.println("Elements of Array R:");


for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}

Output

Question 13
Write a program to accept the year of graduation from school as an integer value from 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]

198 198 199 199 199 200 200 200 200 201
2 7 3 6 9 3 6 7 9 0

import java.util.Scanner;

public class KboatGraduationYear


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l = 0, h = n.length - 1, idx = -1;


while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}

if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}

Output

Question 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:
11

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 KboatAvgMarks


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter number of students: ");
int n = in.nextInt();

int rollNo[] = new int[n];


String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
double avg[] = new double[n];

for (int i = 0; i < n; i++) {


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
avg[i] = (s1[i] + s2[i] + s3[i]) / 3.0;
}

System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}
12

Output

Question 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:
Sum of all even numbers: ..........
Product of all odd numbers: ..........
import java.util.Scanner;

public class KboatDDAEvenOdd


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
}

System.out.println("Sum of all even numbers = " + evenSum);


System.out.println("Product of all odd numbers = " + oddProd);
}
}

Output

Question 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 KboatDepartmentalStore


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
long m[][] = new long[5][6];

for (int i = 0; i < 5; i++) {


System.out.println("Store " + (i + 1) + " Sales");
for (int j = 0; j < 6; j++) {
System.out.print("Enter monthly sale of department " +
(j + 1) + ": ");
m[i][j] = in.nextInt();
}
13

System.out.println("\nMonthly Sale by store: ");


for (int i = 0; i < 5; i++) {
long storeSale = 0;
for (int j = 0; j < 6; j++) {
storeSale += m[i][j];
}
System.out.println("Store " + (i + 1)
+ " Sales: " + storeSale);
}

System.out.println("\nMonthly Sale by Department: ");


for (int i = 0; i < 6; i++) {
long deptSale = 0;
for (int j = 0; j < 5; j++) {
deptSale += m[j][i];
}
System.out.println("Department " + (i + 1)
+ " Sales: " + deptSale);
}
}
}

Output

Question 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 a 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 KboatHotelEnquiry


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String M[][] = new String[5][10];
int i = 0, j = 0;

for (i = 0; i < 5; i++) {


System.out.println("Enter floor " + (i + 1)
+ " guest details:");
for (j = 0; j < 10; j++) {
System.out.print("Guest in room " +
(j + 1) + ": ");
M[i][j] = in.nextLine();
}
}

boolean found = false;


System.out.print("\nEnter guest name to search: ");
String guest = in.nextLine();
for (i = 0; i < 5; i++) {
for (j = 0; j < 10; j++) {
if (M[i][j].equals(guest)) {
found = true;
break;
}
}

if (found)
break;
}

if (found)
14

System.out.println(guest + " is in room number "


+ (j + 1) + " on floor number " + (i + 1));
else
System.out.println(guest +
" is not staying at this hotel");
}
}

Output

Question 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 KboatDDAStudentRecord


{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 40;
final int TOTAL_SUBJECTS = 5;
final String SUBJECT_NAMES[] = {"English", "Hindi",
"Maths", "Science", "Computer Science"};
Scanner in = new Scanner(System.in);
String names[] = new String[TOTAL_STUDENTS];
int marks[][] = new int[TOTAL_STUDENTS][TOTAL_SUBJECTS];
int i = 0, j = 0;

for (i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println("Student " + (i + 1) + " details:");
System.out.print("Name: ");
names[i] = in.nextLine();
for (j = 0; j < TOTAL_SUBJECTS; j++) {
System.out.print("Enter marks in " +
SUBJECT_NAMES[j] + ": ");
marks[i][j] = in.nextInt();
}
in.nextLine();
}

System.out.print("\nEnter name of the student to search: ");


String studentName = in.nextLine();
for (i = 0; i < TOTAL_STUDENTS; i++) {
if (names[i].equals(studentName))
break;
}

if (i == TOTAL_STUDENTS) {
System.out.println("Record for " + studentName
+ " not found");
}
else {
System.out.println("Name: " + names[i]);
int total = 0;
for (j = 0; j < TOTAL_SUBJECTS; j++) {
System.out.println("Marks in " +
SUBJECT_NAMES[j] + ": " + marks[i][j]);
total += marks[i][j];
}
System.out.println("Total Marks: " + total);
}
}
}
15

Output

Question 19
If arrays M and M + N are as shown below, write a program in Java to find the array N.
M = {{-1, 0, 2}, M + N = {{-6, 9, 4},
{-3, -1, 6}, {4, 5, 0},
{4, 3, -1}} {1, -2, -3}}

public class KboatSubtractDDA


{
public static void main(String args[]) {

int arrM[][] = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};

int arrSum[][] = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};

int arrN[][] = new int[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
arrN[i][j] = arrSum[i][j] - arrM[i][j];
}
}

System.out.println("Array N:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arrN[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}

You might also like