0% found this document useful (0 votes)
10 views3 pages

Java Programs for Arrays and Strings

The document contains multiple Java programming assignments focusing on arrays, including creating and manipulating two-dimensional arrays for grades, natural numbers, and matrix operations. It also explains the concept of multidimensional arrays and provides examples of using string methods. Each section includes a program that demonstrates the respective concepts and operations.

Uploaded by

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

Java Programs for Arrays and Strings

The document contains multiple Java programming assignments focusing on arrays, including creating and manipulating two-dimensional arrays for grades, natural numbers, and matrix operations. It also explains the concept of multidimensional arrays and provides examples of using string methods. Each section includes a program that demonstrates the respective concepts and operations.

Uploaded by

harshvardhanp020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT 2

Q1] Write program in java, create two dimension array to store grades of 7

subjects of 10 students of a batch. Find the lowest, highest and average

grade of batch.

Program: Grades of 10 Students and 7 Subjects

public class Grades {


public static void main(String[] args) {
int[][] grades = {
{78, 82, 90, 67, 88, 75, 95},
{55, 60, 65, 70, 75, 80, 85},
{88, 90, 92, 85, 87, 89, 91},
{45, 50, 55, 60, 65, 70, 75},
{90, 93, 95, 97, 99, 85, 88},
{70, 72, 74, 76, 78, 80, 82},
{60, 65, 70, 75, 80, 85, 90},
{50, 55, 60, 65, 70, 75, 80},
{92, 94, 96, 98, 99, 97, 95},
{40, 45, 50, 55, 60, 65, 70}
};

int lowest = grades[0][0];


int highest = grades[0][0];
int sum = 0;
int count = 0;

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


for (int j = 0; j < 7; j++) {
int grade = grades[i][j];
sum += grade;
count++;
if (grade < lowest) lowest = grade;
if (grade > highest) highest = grade;
}
}

double average = (double) sum / count;

[Link]("Lowest grade: " + lowest);


[Link]("Highest grade: " + highest);
[Link]("Average grade: " + average);
}
}

Q2] Write a program in Java to display n terms of natural numbers and their

sum.

2Program: Display n Natural Numbers and Their Sum

import [Link];

public class NaturalNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();

int sum = 0;
[Link]("Natural numbers are:");
for (int i = 1; i <= n; i++) {
[Link](i + " ");
sum += i;
}
[Link]("\nSum = " + sum);
}
}

Q3] What is a Multidimensional array? Write a java program for addition of

two dimensional arrays.

What is a Multidimensional Array?

👉 A multidimensional array means an array having more than one dimension — like a table with rows and
columns.
Example:

int[][] arr = new int[3][3]; // 2D array (3 rows, 3 columns)

Program: Addition of Two 2D Arrays

public class AddMatrices {


public static void main(String[] args) {
int[][] a = {{1, 2, 3}, {4, 5, 6}};
int[][] b = {{7, 8, 9}, {1, 2, 3}};
int[][] sum = new int[2][3];

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


for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}

[Link]("Sum of two matrices:");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}

Q4] Write a program to find multiplication of two matrices.

Program: Multiplication of Two Matrices

public class MultiplyMatrices {


public static void main(String[] args) {
int[][] a = {{1, 2, 3}, {4, 5, 6}};
int[][] b = {{7, 8}, {9, 10}, {11, 12}};
int[][] result = new int[2][2];

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


for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}

[Link]("Multiplication of matrices:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}

Q5] What is an Array? How do you declare and initialize an Array in java?

What is an Array? How to Declare & Initialize

👉 Array is a collection of similar type of elements stored in one variable.


Example:

int[] numbers = new int[5]; // Declaration


int[] marks = {90, 80, 70, 60, 50}; // Initialization

👉 Here marks is an array containing 5 integer values.

Q6] Write a program in java, which implement a task to show use of string

methods like indexOf, substring, lowercase, uppercase

Program: Use of String Methods

public class StringMethods {


public static void main(String[] args) {
String text = "Hello Java Programming";

[Link]("Original String: " + text);


[Link]("Lowercase: " + [Link]());
[Link]("Uppercase: " + [Link]());
[Link]("Substring (6 to 10): " + [Link](6,
10));
[Link]("Index of 'Java': " + [Link]("Java"));
}
}

You might also like