Simple Trick to Learn Linear Search in Java (Integer & String ) - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Simple Trick to Learn Linear Search in Java (Integer & String )

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 : 




click to next :

Sorting and Searching Programs

Start with your Choice

-Important Programs for Freshers

Learn Pattern Programs 

Click to learn Array Programs

Click to Learn MySQL 

for any complaint regarding my Blog please visit contact us page and write what problem you have!

No comments:

Pages