Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its position where it is present in the array. Otherwise display message "not found".
Java Programming Language (Article) (Program)
81
Given Input:
Enter array elements:
1.5 2.3 3.9 4.1 5.6 6.7 7.8 8.0 9.2 10.5 11.3 12.6 13.7 14.8 15.9 16.4 17.5 18.3 19.0 20.1
Enter the number to search: 15.9
Expected Output:
15.9 found at index 14
Program:
import java.util.Scanner;
public class RansariLinearSearch {
public static void main(String args[]) {
Scanner in = new Scanner(System.in); // Create Scanner object for input
double arr[] = new double[20]; // Declare an array of double data type with size 20
int l = arr.length; // Get the length of the array (which is 20)
int i = 0;
// Accept values for the array from the user
System.out.println("Enter array elements: ");
for (i = 0; i < l; i++) {
arr[i] = in.nextDouble(); // Accept each double value from the user and store it in the array
}
// Ask user for the number to search in the array
System.out.print("Enter the number to search: ");
double n = in.nextDouble(); // Accept the number to search for
// Linear search algorithm
for (i = 0; i < l; i++) {
if (arr[i] == n) { // If the current element is equal to the number being searched
break; // Exit the loop when the value is found
}
}
// Check if the number was found or not
if (i == l) {
System.out.println("Not found"); // If the loop goes through all elements and doesn't find the number
} else {
System.out.println(n + " found at index " + i); // If the number was found, display the index
}
in.close(); // Close the Scanner object
}
}
Output:
15.9 found at index 14
If the search value is not found in the array:
Not found
Output:
mathematica
Copy
Edit
Explanation:
- Array Creation: A
double[] array of size 20 is created to store the user's input values.
- User Input: The program prompts the user to enter 20
double values to populate the array.
- Search Input: After filling the array, the program asks the user to enter a value to search for in the array.
- Linear Search: It iterates through the array elements one by one and compares each element with the search value.
- Result: If the value is found, it displays the index where the value is located. If not, it prints "Not found".
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
15.9 found at index 14
Program:
import java.util.Scanner; public class RansariLinearSearch { public static void main(String args[]) { Scanner in = new Scanner(System.in); // Create Scanner object for input double arr[] = new double[20]; // Declare an array of double data type with size 20 int l = arr.length; // Get the length of the array (which is 20) int i = 0; // Accept values for the array from the user System.out.println("Enter array elements: "); for (i = 0; i < l; i++) { arr[i] = in.nextDouble(); // Accept each double value from the user and store it in the array } // Ask user for the number to search in the array System.out.print("Enter the number to search: "); double n = in.nextDouble(); // Accept the number to search for // Linear search algorithm for (i = 0; i < l; i++) { if (arr[i] == n) { // If the current element is equal to the number being searched break; // Exit the loop when the value is found } } // Check if the number was found or not if (i == l) { System.out.println("Not found"); // If the loop goes through all elements and doesn't find the number } else { System.out.println(n + " found at index " + i); // If the number was found, display the index } in.close(); // Close the Scanner object } }
Output:
15.9 found at index 14 If the search value is not found in the array: Not found Output: mathematica Copy Edit
Explanation:
- Array Creation: A
double[]array of size 20 is created to store the user's input values. - User Input: The program prompts the user to enter 20
doublevalues to populate the array. - Search Input: After filling the array, the program asks the user to enter a value to search for in the array.
- Linear Search: It iterates through the array elements one by one and compares each element with the search value.
- Result: If the value is found, it displays the index where the value is located. If not, it prints "Not found".
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.