C Program To Search A Number And Count Its Occurrence In An Array

Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a C program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.

Example: Expected Output

Enter 5 integer numbers
1
5
6
3
5
Enter the number to be searched …
5

5 has appeared at position 2 in the array.
5 has appeared at position 5 in the array.

Final Result: 5 has appeared 2 times in the array.

Visual Representation

search key in array

Video Tutorial: C Program To Search A Number And Count Its Occurrence In An Array


[youtube https://www.youtube.com/watch?v=qLrIu_epQYU]

YouTube Link: https://www.youtube.com/watch?v=qLrIu_epQYU [Watch the Video In Full Screen.]

Source Code: C Program To Search A Number And Count Its Occurrence In An Array

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, key, count = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Enter the number to be searched ...\n");
    scanf("%d", &key);

    printf("\n");

    for(i = 0; i < N; i++)
    {
        if(a[i] == key)
        {
          printf("%d has appeared at position %d in the array.\n", key, i + 1);
          count++;
        }
    }

  printf("\nFinal Result: %d has appeared %d times in the array.\n", key, count);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
9
6
4
Enter the number to be searched …
4

4 has appeared at position 5 in the array.
Final Result: 4 has appeared 1 times in the array.

Output 2:
Enter 5 integer numbers
1
2
3
4
5
Enter the number to be searched …
6

Final Result: 6 has appeared 0 times in the array.

Output 3:
Enter 5 integer numbers
1
5
4
5
2
Enter the number to be searched …
5

5 has appeared at position 2 in the array.
5 has appeared at position 4 in the array.

Final Result: 5 has appeared 2 times in the array.

Logic To Search A Number And Count Its Occurrence In An Array

We ask the user to enter N integer numbers(25 integer numbers according to the problem statement) and store it inside array a[N]. Next we ask the user to input the number to be searched – we store the user input inside variable key.

Inside for loop
We make use of for loop to iterate through entire array. For each iteration we check if the value present at a[i] is equal to value present in key. If it’s true, then we display the position(index value + 1) at which “key” appears and also increment the value of variable count by 1.

After all the iterations of for loop, we display the number of occurrences of key inside the array – value of which is present in variable count.

Explanation With Example

If int a[5] = {1, 5, 6, 3, 5};
key = 5;

ia[i]a[i] == keycount
01FALSE0
15TRUE1
26FALSE1
33FALSE1
45TRUE2

5 has appeared at position 2 in the array.
5 has appeared at position 5 in the array.

Final Result: 5 has appeared 2 times in the array.

Note: We increment the position of key in the array by 1, as users are not used to counting from 0. (Array index starts from 0.)

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert