Az Oop 1B
Az Oop 1B
URK23CO2018
Aim
To create an array to store a given number by the user. Write a java program to find out all the
search array (another input array) and its positions.
Description
Input Main Array: The user inputs numbers into a main array.
Input Search Array: The user inputs numbers into a search array.
Search Algorithm: The program checks each number in the search array and finds all occurrences of
that number in the main array.
Output: It prints each element from the search array along with its positions in the main array.
An array is a data structure that stores a fixed-size sequential collection of elements of the same type.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value. The elements in an array are indexed, starting from 0.
Q1. To create an array to store a given number by the user. Write a java program to find out all the
search array (another input array) and its positions.
Program
import java.util.Scanner;
System.out.println("URK23CO2018");
1
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
mainArray[i] = scanner.nextInt();
searchArray[i] = scanner.nextInt();
// Find and print the positions of search array elements in the main array
if (mainArray[j] == searchValue) {
2
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
found = true;
if (!found) {
System.out.println();
scanner.close();
Output Screenshot:
3
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
Write a program in Java to print duplicate characters from an array? Write a program in Java
to count the number of vowels and consonants in a character array?
Program
import java.util.Scanner;
4
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
}
}
if (count > 1) {
System.out.println(charArray[i] + " appears " + count + " times");
hasDuplicates = true;
}
}
if (!hasDuplicates) {
System.out.println("No duplicate characters found.");
}
scanner.close();
}
}
Output Screenshot
5
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
Q3. Write a java program to copy the given N numbers of one array into another 2 arrays in such
a way that one array must contain the numbers in ascending order and the other must contain in
a descending order.
Program
import java.util.Scanner;
6
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
scanner.close();
}
// Method to check if a string is a valid integer
private static boolean isInteger(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c) && (c != '-' || str.indexOf(c) != 0)) {
return false;
}
}
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
7
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
return false;
}
return true;
}
// Bubble Sort to sort an array in ascending order
public static void bubbleSortAscending(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
}
// Bubble Sort to sort an array in descending order
public static void bubbleSortDescending(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] < array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
8
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018
}
}
Output Screenshot
Result