DEPARTMENT OF
COMPUTER SCIENCE &
Assignment-1
Student Name: Eshan Jaiswal UID: 21BCS3143
Branch: BE-CSE Section/Group:21BCS-643(B)
Semester: 6th Date of Performance:
23.01.24
Subject Name: Programming-Based Learning in JAVA with Lab
Subject Code: 21CSH-319
Questions:-
1) 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
2) Ex1) Array elements are {1,4,34,56,7} and the search element is 90
3) O/P: -1
4) Ex2)Array elements are {1,4,34,56,7} and the search element is 56
5) O/P: 4
Code:-
public class Main{
public static void main(String[] args)
int[] array = {1, 4, 34, 56, 7};
int searchElement1 = 90;
int searchElement2 = 56
int index1 = searchInArray(array, searchElement1);
int index2 = searchInArray(array, searchElement2);
[Link]("For search element " + searchElement1 + ": " + (index1 == -1 ? -1 :
"Not found"));
[Link]("For search element " + searchElement2 + ": " + (index2 == -1 ? -1 :
"Found at index " + index2));
}
DEPARTMENT OF
COMPUTER SCIENCE &
private static int searchInArray(int[] array, int searchElement) {
for (int i = 0; i < [Link]; i++) {
if (array[i] == searchElement) {
return i;
}
}
return -1;
}
}
Output:-
Q2:- Write a program that accepts 2 integers a and b as input and finds the quotient of a/b.
This program may generate an Arithmetic Exception. Use exception handling mechanisms to
handle this exception. In the catch block, print the message as shown in the sample output.
Also illustrate the use of finally block. Print the message “Inside finally block”.
Sample Input and Output 1:
Enter the 2 numbers
5
2
The quotient of 5/2 = 2
DEPARTMENT OF
COMPUTER SCIENCE &
Inside finally block
Sample Input and Output 2:
Enter the 2 numbers
5
0
DivideByZeroException caught
Inside finally block