Linear search is a search that finds an element in the list by searching the element sequentially until the element is found in the list.
💻Example 1 :
#include <stdio.h>
int main() {
int arr[10]={2, 4, 6, 78, 34, 12, 23, 98, 43, 22};
int key = 12;
for(int i=0; i<10; i++)
{
if(arr[i]==key)
{
printf("Element found at %d", i);
break;
}
}
return 0;
}
⚙️ Output :
Element found at 5
💻Example 2 :
#include <stdio.h>
int main() {
int arr[10]={2, 4, 6, 78, 34, 12, 23, 98, 43, 22};
int key;
printf("Enter key: ");
scanf("%d", &key);
for(int i=0; i<10; i++)
{
if(arr[i]==key)
{
printf("%d found at %d index",key, i);
break;
}
}
return 0;
}
⚙️ Output :
Enter key: 12
12 found at 5 index
💻Example 3 :
#include <stdio.h>
int main() {
int arr[100];
int n;
printf("How many elements do you want to add: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Enter value for index %d: ",i);
scanf("%d", &arr[i]);
}
int key;
printf("Enter key: ");
scanf("%d", &key);
for(int i=0; i<n; i++)
{
if(arr[i]==key)
{
printf("%d found at %d index",key, i);
break;
}
}
return 0;
}
⚙️ Output :
How many elements do you want to add: 5
Enter value for index 0: 2
Enter value for index 1: 34
Enter value for index 2: 52
Enter value for index 3: 12
Enter value for index 4: 22
Enter key: 52
52 found at 2 index
A flag variable is usually given two values 0 (False) and 1 (True) . So, its used as a Boolean variable.
💻Example 4 :
#include <stdio.h>
int main() {
int arr[100];
int n;
printf("How many elements do you want to add: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Enter value for index %d: ",i);
scanf("%d", &arr[i]);
}
int key;
printf("Enter key: ");
scanf("%d", &key);
int flag = 0;
for(int i=0; i<n; i++)
{
if(arr[i]==key)
{
printf("%d found at %d index",key, i);
flag=1;
break;
}
}
if(flag==0)
{
printf("Element not found");
}
return 0;
}
⚙️ Output :
How many elements do you want to add: 4
Enter value for index 0: 12
Enter value for index 1: 24
Enter value for index 2: 36
Enter value for index 3: 48
Enter key: 36
36 found at 2 index
💻Example 5 :
#include <stdio.h>
int main() {
int arr[100];
int n;
printf("How many elements do you want to add: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Enter value for index %d: ",i);
scanf("%d", &arr[i]);
}
int key;
printf("Enter key: ");
scanf("%d", &key);
int flag = 0;
for(int i=0; i<n; i++)
{
if(arr[i]==key)
{
printf("%d found at %d index",key, i);
flag=1;
break;
}
}
if(flag==0)
{
printf("Element not found");
}
return 0;
}
⚙️ Output :
How many elements do you want to add: 4
Enter value for index 0: 12
Enter value for index 1: 24
Enter value for index 2: 36
Enter value for index 3: 48
Enter key: 40
Element not found
Sum of Array Elements
💻Example 6 :
#include <stdio.h>
int main() {
int marks[] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i=0; i<5; i++)
{
sum = sum + marks[i];
}
printf("Sum of marks: %d",sum);
return 0;
}
⚙️ Output :
Sum of marks: 150
Largest Element from the Array
💻Example 7 :
#include <stdio.h>
int main() {
int arr[6];
printf("Enter 6 values: \n");
for(int i=0; i<6; i++)
{
printf("Enter value for index %d: ", i);
scanf("%d", &arr[i]);
}
int max = arr[0];
for(int i=0; i<6; i++)
{
if(arr[i]>max)
{
max = arr[i];
}
}
printf("Maximum value from array is: %d", max);
return 0;
}
⚙️ Output :
Enter 6 values:
Enter value for index 0: 12
Enter value for index 1: 24
Enter value for index 2: 56
Enter value for index 3: 23
Enter value for index 4: 6
Enter value for index 5: 58
Maximum value from array is: 58
🏠Homework
Write a C program to find smallest element from an array