1. Write a program to initialize an integer array and print the sum and average of the array.
1: import java.util.Arrays; 2: public class Assignment01 { 3: public static void main(String[] args) { 4: // TODO Auto-generated method stub 5: int[] array = {19, 14, 20, 18, 2}; 6: int sum = 0; 7: System.out.println("Array: "+Arrays.toString(array)); //print Array 8: for(int i = 0; i < array.length; i++) { //sum 9: sum = sum + array[i]; 10: } 11: System.out.println("The sum of the array is: "+sum); 12: double avg = (sum/array.length); //average 13: System.out.println("The average of the array is: "+avg); 14: } 15: }
2. Write a program to initialize an integer array and find the maximum and minimum value of the array.
1: import java.util.Arrays;
2: public class Assignment02 {
3: public static void main(String[] args) {
4: // TODO Auto-generated method stub
5: int[] array = {19, 14, 20, 18, 2};
6: System.out.println("Array: "+Arrays.toString(array)); //print Array
7: int max = array[0]; //max value
8: for(int i = 1; i < array.length; i++) {
9: if(array[i] > max)
10: max = array[i];
11: }
12: System.out.println("The maximum value of Array is: "+max);
13: int min = array[0]; //min value
14: for(int i = 1; i < array.length; i++) {
15: if(array[i] < min)
16: min = array[i];
17: }
18: System.out.println("The minimum value of Array is: "+min);
19: }
20: }
3. Write a program to initialize an integer array with values and check if a given number is present in the array or not.
If the number is not found, it will print -1 else it will print the index value of the given number in the array.
Example 1: If the Array elements are {1,4,34,56,7} and the search element is 90, then the output expected is -1.
Example 2: If the Array elements are {1,4,34,56,7} and the search element is 56, then the output expected is 3.
1: public class Assignment03 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: int[] array = {1, 4, 34, 56, 7};
5: int key = 92;
6: int i, flag = 0;
7: for(i = 0; i < array.length; i++) {
8: if(array[i] == key) {
9: flag = 1;
10: break;
11: }
12: }
13: if(flag == 1) {
14: System.out.println(i+1);
15: }
16: else {
17: System.out.println("-1");
18: }
19: }
20: }
4. Initialize an integer array with ASCII values and print the corresponding character values in a single row.
1: public class Assignment04 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: int[] ascii = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90};
5: for(int i = 0; i < ascii.length; i++) {
6: System.out.print((char)ascii[i]+" ");
7: }
8: }
9: }
5. Write a program to find the largest 2 numbers and the smallest 2 numbers in the given array.
1: public class Assignment05 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: int[] array = {19, 14, 20, 18, 2}; //sort
5: for(int i = 0; i < array.length-1; i++) {
6: for(int j = 0; j < array.length-1; j++) {
7: if(array[j] > array[j+1]) {
8: int temp = array[j+1];
9: array[j+1] = array[j];
10: array[j] = temp;
11: }
12: }
13: }
14: System.out.println("Largest two numbers are "+array[array.length-1]+", "+array[array.length-2]);
15: System.out.println("Smallest two numbers are "+array[0]+", "+array[1]);
16: }
17: }
6. Write a program to initialize an array and print them in sorted order.
1: import java.util.Arrays;
2: public class Assignment06 {
3: public static void main(String[] args) {
4: // TODO Auto-generated method stub
5: int[] array = {19, 14, 20, 18, 2};
6: Arrays.sort(array); //sort inbuilt function
7: /*for(int i = 0; i < array.length-1; i++) { //sort logic
8: for(int j = 0; j < array.length-1; j++) {
9: if(array[j] > array[j+1]) {
10: int temp = array[j+1];
11: array[j+1] = array[j];
12: array[j] = temp;
13: }
14: }
15: }
16: */
17: System.out.print("Sorted Array : ");
18: for(int element : array) {
19: System.out.print(element + " ");
20: }
21: }
22: }
7. Write a program to remove the duplicate elements in an array and print the same.
Example:
I/P:{12,34,12,45,67,89}
O/P:{12,34,45,67,89}
1: import java.util.Arrays;
2: public class Assignment07 {
3: public static void main(String[] args) {
4: // TODO Auto-generated method stub
5: int[] array = {12, 34, 12, 45, 67, 89};
6: Arrays.sort(array); //sort
7: int[] temp = new int[array.length];
8: int j = 0; //Using temporary array
9: for (int i = 0; i < array.length-1; i++){
10: if (array[i] != array[i+1]){
11: temp[j++] = array[i];
12: }
13: }
14: temp[j++] = array[array.length-1];
15: for (int i = 0; i < j; i++){
16: System.out.print(temp[i]+" "); //last element
17: }
18: }
19: }
8. Write a program to print the sum of the elements of an array following the given below condition.
If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the other numbers for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
1: public class Assignment08 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: int[] array= {10, 3, 6, 1, 2, 7, 9};
5: int sum = 0;
6: int flag = 0;
7: for(int i = 0; i < array.length; i++) {
8: if(array[i] == 6)
9: flag = 1;
10: else if(array[i] == 7) {
11: flag = 0;
12: i++;
13: }
14: if(flag != 1)
15: sum = sum + array[i];
16: }
17: System.out.println(sum);
18: }
19: }
9. Write a program to reverse the elements of a given 2*2 array. Four integer numbers needs to be passed as Command-Line arguments.
Example1:
C:\>java Sample 1 2 3
O/P: Please enter 4 integer numbers
Example2:
C:\>java Sample 1 2 3 4
O/P:
The given array is :
1 2
3 4
The reverse of the array is :
4 3
2 1
1: public class Assignment09 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: if(args.length != 4) {
5: System.out.println("Please enter 4 integer numbers");
6: }
7: else {
8: int[][] array = new int[2][2];
9: int x = 0;
10: //storing elements
11: for (int i = 0; i < array.length; i++) {
12: for (int j = 0; j < array.length; j++) {
13: array[i][j] = Integer.parseInt(args[x++]);
14: }
15: }
16: System.out.println("The given array is :");
17: for (int i = 0; i < array.length; i++) {
18: for (int j = 0; j < array.length; j++) {
19: System.out.print(array[i][j] + "\t");
20: }
21: System.out.println();
22: }
23: System.out.println("The reverse of the array is :");
24: for(int i = array.length-1; i >= 0; i--) {
25: for(int j = array.length-1; j >= 0; j--) {
26: System.out.print(array[i][j] + "\t");
27: }
28: System.out.println();
29: }
30: }
31: }
32: }
10. Write a program to find the biggest number in a 3*3 array. The program is supposed to receive 9 integer numbers as command-line arguments.
Example1:
C:\>java Sample 1 2 3
O/P: Please enter 9 integer numbers
Example2:
C:\>java Sample 1 23 45 55 121 222 56 77 89
O/P:
The given array is :
1 23 45
55 121 222
56 77 89
The biggest number in the given array is 222
1: public class Assignment10 {
2: public static void main(String[] args) {
3: // TODO Auto-generated method stub
4: if(args.length != 9) {
5: System.out.println("Please enter 9 integer numbers");
6: }
7: else {
8: int[][] array = new int[3][3];
9: int x = 0;
10: //storing elements
11: for (int i = 0; i < array.length; i++) {
12: for (int j = 0; j < array.length; j++) {
13: array[i][j] = Integer.parseInt(args[x++]);
14: }
15: }
16: System.out.println("The given array is :");
17: for (int i = 0; i < array.length; i++) {
18: for (int j = 0; j < array.length; j++) {
19: System.out.print(array[i][j] + "\t");
20: }
21: System.out.println();
22: }
23: int max = 0;
24: for(int i = 0; i < array.length; i++) {
25: for(int j = 0;j < array.length; j++) {
26: if(array[i][j] > max)
27: max = array[i][j];
28: }
29: }
30: System.out.println("The biggest number in the given array is "+max);
31: }
32: }
33: }