C Language | Arrays 8

Question:16
(Sequential search/linear search/searching)
C program to take input for 10 elements using an array further take input for an element to search check and print whether the element is present in the array or not?
Sol:

#include<stdio.h>

int main()
{
    int a[30],i,n,m,z=0;
    /* clrscr(); */
    printf("Enter the total number of elements ");
    scanf("%d",&m);
    printf("enter the array elements \n");
    for(i=0;i<m;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    printf("\nenter the value of the element to search ");
    scanf("%d",&n);
    for(i=0;i<m;i++)
    {
        if (n==a[i])
        {
            z=1;
            break;
        }
    }
    if (z==1)
        printf("Element is present");
    else
        printf("Elements is not present");
    return 0;
}
/* Output */

Enter the total number of elements 5
enter the array elements
enter the 0 element 25
enter the 1 element 63
enter the 2 element 21
enter the 3 element 5
enter the 4 element 63

enter the value of the element to search 21
Element is present

Question:17
(Search and count)
C program to take input for 10 elements using an array further take input for an element to search check and print how many times the element is present in the array?
Sol:

#include<stdio.h>

int main()
{
    int a[30],i,n,m,z=0;
    /* clrscr(); */
    printf("Enter the total number of elements ");
    scanf("%d",&m);
    printf("enter the array elements \n");
    for(i=0;i<m;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    printf("\nenter the value of the element to search ");
    scanf("%d",&n);
    for(i=0;i<m;i++)
    {
        if (n==a[i])
        {
            z=z+1;
        }
    }
    if (z==0)
        printf("Element is not present");
    else
        printf("Elements is present %d times",z);
    return 0;
}
/* Output */

Enter the total number of elements 10
enter the array elements
enter the 0 element 25
enter the 1 element 63
enter the 2 element 2
enter the 3 element 10
enter the 4 element 3
enter the 5 element 6
enter the 6 element 10
enter the 7 element 54
enter the 8 element 25
enter the 9 element 10

enter the value of the element to search 10
Elements is present 3 times

Question:18
(Search count and print position)
C program to take input for 10 elements using an array further take input for an element to search, if element is present, print its count and position in the array?
Sol:

#include<stdio.h>

int main()
{
    int a[30],i,n,m,z=0;
    /* clrscr(); */
    printf("Enter the total number of elements ");
    scanf("%d",&m);
    printf("enter the array elements \n");
    for(i=0;i<m;i++)
    {
        printf("enter the %d element ",i);
        scanf("%d",&a[i]);
    }
    printf("\nenter the value of the element to search ");
    scanf("%d",&n);
    for(i=0;i<m;i++)
    {
        if (n==a[i])
        {
            z=z+1;
            printf("Element found at location %d\n",i+1);
        }
    }
    if (z==0)
        printf("Element is not present");
    else
        printf("Elements is present %d times",z);
    return 0;
}
/* Output */

Enter the total number of elements 10
enter the array elements
enter the 0 element 25
enter the 1 element 10
enter the 2 element 6
enter the 3 element 3
enter the 4 element 10
enter the 5 element 24
enter the 6 element 8
enter the 7 element 10
enter the 8 element 95
enter the 9 element 6

enter the value of the element to search 10
Element found at location 2
Element found at location 5
Element found at location 8
Elements is present 3 times

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects