Explanation of Linear Search in Java Integer and String
public class LinearSearch {
public static void main(String[] args) {
/*int arr[]={5,6,7,8,9,2,10};
int number=10; // for integer value
int temp=0;
for (int i=0;i<arr.length;i++){
if (arr[i]==number){
System.out.println("Number present at "+i+" position");
temp=temp+1;
}
}
if (temp==0){
System.out.println("number not present in array");
}
Output :
_________________________________________
Linear Search for String
String str[]={"sagar","kumar","ravi","sonu","monu"};
String name ="ravi";
int temp=0;
for (int i=0;i<str.length;i++){
if (str[i].equals(name)){
System.out.println("Name is present at "+i+ " position");
temp=temp+1;
}
}
if (temp==0){
System.out.println("not available");
}
}
}
Output :
public class LinearSearch {
public static void main(String[] args) {
/*int arr[]={5,6,7,8,9,2,10};
int number=10; // for integer value
int temp=0;
for (int i=0;i<arr.length;i++){
if (arr[i]==number){
System.out.println("Number present at "+i+" position");
temp=temp+1;
}
}
if (temp==0){
System.out.println("number not present in array");
}
Output :
_________________________________________
Linear Search for String
String str[]={"sagar","kumar","ravi","sonu","monu"};
String name ="ravi";
int temp=0;
for (int i=0;i<str.length;i++){
if (str[i].equals(name)){
System.out.println("Name is present at "+i+ " position");
temp=temp+1;
}
}
if (temp==0){
System.out.println("not available");
}
}
}
Output :
click to next :
Sorting and Searching Programs
Start with your Choice
-Important Programs for Freshers
- Palindrome Number
- Prime Number
- Swapping Number
- Leap Year
- Odd and Even Number
- Fibonacci Series
- Armstrong Number
- Factorial Number
- Print Table
Learn Pattern Programs
- Alphabet pattern program in java
- Star Pattern Programs in Java
- Square pattern programs in java
- Diamond Star pattern programs in java
- Number Pattern Programs in java
- Diagonal pattern program in java
Click to learn Array Programs
- 4 ways to print Array in Java
- 2-D Array in Java
- Anonymous Array in Java
- Single Dimensional Array in Java
- Find Second Largest Number in Array
- Delete an Element in Array in Java
- Common Element in Array in Java
- Missing Element in Array
- Insert Element in Array
- Reverse Array in Java
- Merge Two Array in Java
- Smallest and Largest Elements in Array
- Find Odd and Even Number in Array
No comments:
Post a Comment