Computer Applications
Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements.
Example:
A[ ][ ]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4 + 6 + 2 + 4 + 2 = 18
Sum of odd elements = 5 + 5 + 3 + 5 = 18
Java
Java String Handling
ICSE Sp 2024
86 Likes
Answer
import java.util.Scanner;
public class KboatDDASpArr
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[][] = new int[3][3];
long evenSum = 0, oddSum = 0;
System.out.println("Enter the elements of 3 x 3 DDA: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] % 2 == 0)
evenSum += arr[i][j];
else
oddSum += arr[i][j];
}
}
System.out.println("Sum of even elements = " + evenSum);
System.out.println("Sum of odd elements = " + oddSum);
if (evenSum == oddSum)
System.out.println("Special Array");
else
System.out.println("Not a Special Array");
}
}
Variable Description Table
Program Explanation
Output
![BlueJ output of Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements. Example: A[ ][ ]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}}; Sum of even elements = 4 + 6 + 2 + 4 + 2 = 18 Sum of odd elements = 5 + 5 + 3 + 5 = 18 BlueJ output of Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements. Example: A[ ][ ]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}}; Sum of even elements = 4 + 6 + 2 + 4 + 2 = 18 Sum of odd elements = 5 + 5 + 3 + 5 = 18](https://cdn1.knowledgeboat.com/img/abp10/1/sp-2024-p6.jpg)
Answered By
35 Likes
Related Questions
Define a class to accept values in integer array of size 10. Sort them in an ascending order using selection sort technique. Display the sorted array.
Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.
Input: robotics
Output: ROBOTICS
Number of vowels: 3Define a class to accept a 3 digit number and check whether it is a duck number or not.
Note: A number is a duck number if it has zero in it.Example 1:
Input: 2083
Output: InvalidExample 2:
Input: 103
Output: Duck numberDefine a class to overload the method display as follows:
void display( ): To print the following format using nested loop
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
void display(int n): To print the square root of each digit of the given number.
Example:
n = 4329
Output – 3.0
1.414213562
1.732050808
2.0