Write a program in C++ that inputs data into an array. It inputs a value from the user and searches this value in the array using sequential search. If the entered value is found in the array, it displays the position of the value in the array; otherwise, it displays the message “Value not found”.
So let’s Write Code:
Code:
Write a program in C++ that inputs data into an array. It inputs a value from the user and searches this value in the array using sequential search.
#include"iostream"
using namespace std;
main()
{
int arr[5], n, i, pos;
i = 0;
while(i<=4)
{
cout<<"Enter value in element "<<i<<"? ";
cin>>arr[i];
i++;
}
pos = 0;
cout<<"Enter any value ? ";
cin>>n;
i = 0;
while(i<=4)
{
if(n == arr[i])
{
pos = i + 1;
break;
}
i++;
}
if(pos == 0)
cout<<"Value not found "<<endl;
else
cout<<"Value found at position = "<<pos<<endl;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well😇 https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: 👉https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: 👉https://myustaadg.com/category/programming-exercises/arrays-exercises/