// Cpp program to linearly search x in array #include <iostream> #include <vector> using…
- User
// Cpp program to linearly search x in array #include #include using namespace std;
int search(vector& arr, int x) { for (int i = 0; i < arr.size(); i++) if (arr[i] == x) return i; return -1; }
int main() { vector arr = {2, 3, 4, 10, 40}; int x = 10; int res = search(arr, x); if (res == -1) cout << "Not Present"; else cout << "Present at Index " << res; return 0; } Give me code in Python, Java, C#, JavaScript
Remember like Cpp is for C++ and at start replace Cpp with language name
without changing variable name and also do not change comments
have one line space after code when comment is written.
don’t use class if needed then name it GfG
don’t use public if not needed.
and In C program don’t use #define and malloc and calloc instead use simple array
- Assistantpython
# Python program to linearly search x in array def search(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -1 def main(): arr = [2, 3, 4, 10, 40] x = 10 res = search(arr, x) if res == -1: print("Not Present") else: print("Present at Index", res) if __name__ == "__main__": main()