0% found this document useful (0 votes)
31 views

Assignment 2 (Done)

The document contains 8 programming tasks that involve working with arrays in Java. The tasks cover initializing arrays, looping through arrays, modifying array values, concatenating arrays, finding maximum/minimum values, and working with multi-dimensional arrays.

Uploaded by

ansar20042009
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)
31 views

Assignment 2 (Done)

The document contains 8 programming tasks that involve working with arrays in Java. The tasks cover initializing arrays, looping through arrays, modifying array values, concatenating arrays, finding maximum/minimum values, and working with multi-dimensional arrays.

Uploaded by

ansar20042009
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/ 4

Assignment 2

Task 1
public class task1 {
public static void main(String[] args) {
// Declare and initialize an integer array with a size of 10
int[] intArray = new int[10];
// Declare and initialize a double array with a size of 10
double[] doubleArray = new double[10];
}
}

Task 2
public class task2 {
// Declare a public static variable named 'strings' of type String[],
initialized with an array of 5 elements.
public static String[] strings = new String[5];
public static void main(String[] args) {
// Loop through the array indices from 0 to 4
for (int i = 0; i < 5; i++) {
// Check if the current index is even
if (i % 2 == 0) {
// If the index is even, assign the value "Even" to the
corresponding element in the 'strings' array
strings[i] = "Even";
} else {
// If the index is odd, assign the value "Odd" to the
corresponding element in the 'strings' array
strings[i] = "Odd";
}
}
// Loop through the 'strings' array to print out the values
for (int i = 0; i < strings.length; i++) {
// Print the value at the current index in the 'strings' array
System.out.println(strings[i]);
}
}
}

Task 3
public class task3 {
public static void main(String[] args) {
// Initialize an array with values from 1 to 10
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Loop through each element in the array
for (int i = 0; i < array.length; i++) {
// Check if the current element is an even number
if (array[i] % 2 == 0) {
// If it's even, change its sign
array[i] = -array[i];
}
}
// Display modified array using for loop
for (int value : array) {
System.out.print(value + " ");
}
}
}
Task 4
public class task4 {
// Public static variables firstArray and secondArray of type int[]
public static int[] firstArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public static int[] secondArray = {11, 12, 13, 14, 15, 16, 17, 18, 19,
20};

// Public static variable resultArray of type int[]


public static int[] resultArray;
public static void main(String[] args) {
// Initialize resultArray with the length of both firstArray and
secondArray
resultArray = new int[firstArray.length + secondArray.length];
// Copy the contents of firstArray to resultArray using loop
for (int i = 0; i < firstArray.length; i++) {
resultArray[i] = firstArray[i];
}
// Copy the contents of secondArray to resultArray using loop
for (int i = 0; i < secondArray.length; i++) {
resultArray[firstArray.length + i] = secondArray[i];
}
// Display the resultArray
System.out.print("resultArray: ");
for (int value : resultArray) {
System.out.print(value + " ");
}
}
}
Task 5
import java.util.Scanner;
public class task5 {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the number N
System.out.print("Enter the number N: ");
int N = scanner.nextInt();
// Create an array
int[] array = new int[N];
// Prompt the user to enter N integers and store them in the array
System.out.println("Enter " + N + " integers:");
for (int i = 0; i < N; i++) {
array[i] = scanner.nextInt();
}
// Assume the first element is the maximum number
int maxNumber = array[0];
// Loop through the array to find the maximum number
for (int i = 1; i < N; i++) {
if (array[i] > maxNumber) {
maxNumber = array[i];
}
}
// Display the maximum number among the array elements
System.out.println("The maximum number in the array is: " +
maxNumber);
}
}
Task 6
public class task6 {
public static void main(String[] args) {
// Create a two-dimensional array to store the multiplication table
int[][] MULTIPLICATION_TABLE = new int[10][10];
// Fill the table with values from 1*1 to 10*10
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
// Store the product of i and j in the corresponding position
MULTIPLICATION_TABLE[i - 1][j - 1] = i * j;
}
}
// Display the table
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// Print each element of the table followed by a space
System.out.print(MULTIPLICATION_TABLE[i][j] + " ");
}
// Move to the next line after printing a row of the table
System.out.println();
}
}
}

Task 7
public class task7 {
public static void main(String[] args) {
// Initialize a two-dimensional array named result with 10 rows
int[][] result = new int[10][];
// Fill the two-dimensional array with values based on the sum of
indices
for (int i = 0; i < result.length; i++) {
// Initialize each row with a different number of columns
result[i] = new int[i + 1];
// Fill the array elements with the sum of row and column index
for (int j = 0; j < result[i].length; j++) {
result[i][j] = i + j;
}
}
// Print the two-dimensional array using loop
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println(); // Move to the next line after printing
each row
}
}
}
Task 8
import java.util.Scanner;
public class task8 {
// Declare public static variable multiArray of type int[]
public static int[][] multiArray;
public static void main(String[] args) {
// Read the number N from the keyboard (the number of rows in the
array) using scanner
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows (N): ");
int N = scanner.nextInt();
// Prompt the user to enter integers for an array using loop
System.out.println("Enter " + N + " numbers:");
int[] sizes = new int[N];
for (int i = 0; i < N; i++) {
sizes[i] = scanner.nextInt();
}
// Initialize the two-dimensional multiArray using loop
multiArray = new int[N][];
for (int i = 0; i < N; i++) {
multiArray[i] = new int[sizes[i]];
}
// Print the multiArray using nested loops
System.out.println("multiArray:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < multiArray[i].length; j++) {
System.out.print("[ ]");
}
System.out.println();// Move to the next line after printing each
row
}
}
}

You might also like