Computer Applications
Define a class to accept 10 integers in an array, search for the given value using the Linear Search technique and print appropriate messages.
Java
Java Arrays
6 Likes
Answer
import java.util.Scanner;
public class LinearSearchArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the value to search: ");
int key = sc.nextInt();
int i = 0;
for (i = 0; i < 10; i++) {
if (arr[i] == key) {
break;
}
}
if (i < 10)
System.out.println("Value found at position: " + (i + 1));
else
System.out.println("Value not found in the array");
}
}Output


Answered By
2 Likes
Related Questions
Define a class to accept values into a 3x3 integer array and print the product of each row elements.
Example:
3 1 2 4 2 1 5 1 2 Output:
Row 0 – 6
Row 1 – 8
Row 2 – 10Define a class to overload the method transform as follows:
int transform(int n) – to return the sum of the digits of the given number
Example: n = 458
output : 17void transform(String s) – to convert the given String to upper case and print
Example: if S = “Blue”
Output : BLUEvoid transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.
Example: if ch = ‘@’
Output :@@@ @@@ @@@Define a class to accept a string. Check if it is a Special String or not.
A String is Special if the number of vowels equals to the number of consonants.
Example: MINE
Number of vowels = 2
Number of Consonants = 2Define a class to accept a number and check if the sum of the first digit and the last digit is an even number or an odd number.
Example:
N = 2396 N = 9316 First digit: 2 9 Last digit: 6 6 Sum: 8 15 Output: Sum is even Sum is odd