20_10

Write a C program to search a number in an array using Linear search algorithm.

Source code:

 /*A Linear search is a searching algorithm which is used to find whether a given number is present in an array. It is also known as sequential search. It is straightforward and keep on comparing each element with the element to search until it is found or the list ends. First an array of size 10 will be taken. We will fill it by reading 10 integers. Then the key integer to be searched will be given as input.   The test case total number of values are 11. The first 10 values are to fill up the array. If it is found then output would return it and it's position.  */


#include <stdio.h>

int main()

{

    int array[10], search, i, n=10;


  /* Read array */

printf("Enter the array:");

    for (i = 0; i < n; i++)

    scanf("%d", &array[i]);


  /* Read the interger to be searched */

printf("Enter the search element:");

    scanf("%d", &search);

for (i = 0; i < n; i++)

    {

    if (array[i] == search)     /* if required element found */

        {

        printf("%d is found at position %d.", search, i+1);

          break;

      }

    }

    if (i == n)

    printf("%d is not there in the array.", search);

return 0;

}

Output:

C program to search a number in an array using Linear search algorithm




No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...