Linear Search in C, C++, JAVA, PHP, Python3 and C#

Today, We want to share with you linear search in c, C++, JAVA, PHP, Python3 and C#.In this post we will show you search program in c using flag, hear for search in data structure we will give you demo and example for implement.In this post, we will learn about int to string c++ with an example.

Linear Search Examples :

There are the Following The simple About How to Implement Linear Search in C? Full Information With Example and source code.

As I will cover this Post with live Working example to develop Linear Search Algorithm, so the C Program for finding number in search is used for this example is following below.

Query: Given simple Example of an array productids[] of n elements, fully source code a function to search a given element x in productids[].

Using C++ code Example

#include  
using namespace std; 

int linearsearchmyfun(int productids[], int n, int x) 
{ 
	int i; 
	for (i = 0; i < n; i++) 
		if (productids[i] == x) 
			return i; 
	return -1; 
} 

int main(void) 
{ 
	int productids[] = { 2, 3, 4, 10, 40 }; 
	int x = 10; 
	int n = sizeof(productids) / sizeof(productids[0]); 
	int response = linearsearchmyfun(productids, n, x); 
(response == -1)? cout<<"Element is not present in array"
				: cout<<"Element is present at index " <

Using C code Example

#include  

int linearsearchmyfun(int productids[], int n, int x) 
{ 
	int i; 
	for (i = 0; i < n; i++) 
		if (productids[i] == x) 
			return i; 
	return -1; 
} 

int main(void) 
{ 
	int productids[] = { 2, 3, 4, 10, 40 }; 
	int x = 10; 
	int n = sizeof(productids) / sizeof(productids[0]); 
	int response = linearsearchmyfun(productids, n, x); 
	(response == -1) ? printf("Element is not present in array") 
				: printf("Element is present at index %d", 
							response); 
	return 0; 
} 

Using Java code Example

class PAKAINFO 
{ 
public static int linearsearchmyfun(int productids[], int x) 
{ 
	int n = productids.length; 
	for(int i = 0; i < n; i++) 
	{ 
		if(productids[i] == x) 
			return i; 
	} 
	return -1; 
} 

public static void main(String args[]) 
{ 
	int productids[] = { 2, 3, 4, 10, 40 }; 
	int x = 10; 
	
	int response = linearsearchmyfun(productids, x); 
	if(response == -1) 
		System.out.print("Element is not present in array"); 
	else
		System.out.print("Element is present at index " + response); 
} 
} 

Using Python3 code Example

def linearsearchmyfun(productids, n, x): 

	for i in range (0, n): 
		if (productids[i] == x): 
			return i; 
	return -1; 

# Driver Code 
productids = [ 2, 3, 4, 10, 40 ]; 
x = 10; 
n = len(productids); 
response = linearsearchmyfun(productids, n, x) 
if(response == -1): 
	print("Element is not present in array") 
else: 
	print("Element is present at index", response); 

Using C# code Example

using System; 

class PAKAINFO 
{ 
	public static int linearsearchmyfun(int[] productids, int x) 
	{ 
		int n = productids.Length; 
		for(int i = 0; i < n; i++) 
		{ 
			if(productids[i] == x) 
				return i; 
		} 
		return -1; 
	} 
	
	public static void Main() 
	{ 
		int[] productids = { 2, 3, 4, 10, 40 }; 
		int x = 10; 
		
		int response = linearsearchmyfun(productids, x); 
		if(response == -1) 
			Console.WriteLine("Element is not present in array"); 
		else
			Console.WriteLine("Element is present at index "+ response); 
	} 
} 

Using PHP code Example

 

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about linear search program.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment