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

C Program To Segregate 0s and 1s In An Array using Swapping Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using Swapping method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Counting Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
1
0
1
0
1
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
0
0
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Swapping Method


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

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Swapping Method

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, left = 0, right = N - 1;

    printf("Enter %d elements(0 or 1)\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0 and 1 as input\n");
            i--;
        }
    }

    while(left < right)
    {
        while(a[left]   == 0)
            left++;

        while(a[right] == 1)
            right--;

        if(left < right)
        {
            a[left++] = 0;
            a[right--] = 1;
        }
    }

    printf("\nArray after sorting 0's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
1
0
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Swapping Method

We ask the user to input N integer numbers and store it inside a[N]. We assign 0(the first index of any array) to variable left and N – 1(the last index of any array) to variable right.

The outer while loop executes until left < right. Inside we write another while loop to check from left, if a[left] is 0. If its already 0, then its already in sorted order, so we don’t swap it. We simply increment the value of left by one position. Control exits this first inner while loop when a[left] is not equal to 0.

Similarly, we check if a[right] is 1. If its 1, we decrement the value of right by 1. When a[right] is not equal to 1, the control exits.

Now variable “left” will have the index position from left where there is number 1. And variable “right” will have the index position from right where there is number 0. Now we need to swap it.

We check if value of left is still less than the value of right. If true, we store 0 at a[left] and 1 at a[right].

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

lefta[left]righta[right]Swap
0041NO
1130YES
2121NO

a[5] after sorting 0’s to left and 1’s to right:
a[5] = {0, 0, 1, 1, 1};

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

C Program To Segregate 0s and 1s In An Array using Counting Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using counting method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Swapping Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
0
1
1
1
1
0
0
1

Array after segregating o’s to left and 1’s to right
0
0
0
0
1
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Counting Method


[youtube https://www.youtube.com/watch?v=gHW6WI-5724]

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Counting Method

#include<stdio.h>

#define N 5

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

    printf("Enter %d elements(0 or 1)\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0's and 1's as input\n");
            i--;
        }
        else if(a[i] == 0)
            count++;
    }

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

    printf("\nArray after segregating o's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
0
1
1
0
0

Array after segregating o’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Counting Method

We ask the user to input N integer numbers and store it inside a[N]. Next we iterate through each element of the array and if the user input is 0, then we increment the value of variable count by 1. After completing the iterations, variable count will have the number of 0’s input by the user.

Modifying the Array
Since user is allowed to either enter 0 or 1. So if we know the number of 0’s input by the user, we can easily calculate the number of 1’s in the array, using the formula:

N – count_0 = count_1
where, N is array size.

Now that we know the exact number of 0’s and 1’s input by the user, we re-enter the 0’s and 1’s as needed by the problem statement. That is, we arrange all the 0’s to left and 1’s to the right.

Number of 0’s plus the number of 1’s should be equal to the array size.
count_0 + count_1 = N;

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

ia[i]count
001
11
21
302
41

count = 2;
That is, the number of 0’s in user input array is 2. So the number of 1’s is:
array_size – count
5 – 2 = 3
So there are three 1’s and two 0’s.

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

Once we have this exact data. We can arrange Two 0’s from left of array, i.e., a[0] and a[1]. And three 1’s from a[2], a[3] and a[4].

This is how we segregate 0’s to left and 1’s to the right of an array, using counting method.

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

C program To Count and Display Even and Odd Elements of An Array

Lets write a C program to display even and odd elements of an array, along with their count.

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array

How To Determine Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2.
Ex: num % 2 == 0

An odd number is an integer that is not exactly divisible by 2.
Ex: num % 2 != 0

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

Even numbers in the array are …
2
4
6
8
10

Odd numbers in the array are …
1
3
5
7
9

Total Even numbers: 5
Total Odd numbers: 5

Visual Representation

count and display even and odd elements of array

Video Tutorial: C program To Count and Display Even and Odd Elements of An Array


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

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

Source Code: C program To Count and Display Even and Odd Elements of An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0;

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

    printf("\n\nEven numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }

    printf("\nOdd numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }

    printf("\n\nTotal Even numbers: %d", even);
    printf("\nTotal Odd numbers: %d\n", odd);

    printf("\n");

    return 0;
}

Output 1:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19

Even numbers in the array are …
10
12
14
16
18

Odd numbers in the array are …
11
13
15
17
19

Total Even numbers: 5
Total Odd numbers: 5

Output 2:
Enter 10 integer numbers
65
12
4
7
87
2
36
45
78
98

Even numbers in the array are …
12
4
2
36
78
98

Odd numbers in the array are …
65
7
87
45

Total Even numbers: 6
Total Odd numbers: 4

Logic To Count and Display Even and Odd Elements of An Array

We ask the user to enter N integer number and store it in array variable a[N]. We iterate or loop through the user input array elements one by one and check if the selected element is perfectly divisible by 2. If true, then its even number, so we display the number/element and keep count of number of even numbers.

Similarly, we loop through the user input array elements one by one once again, and check if the selected element is not perfectly divisible by 2. If true, then its odd number. So we display the odd number and keep the count of number of odd elements.

Once both the iterations are complete, we display the number of even and odd elements count which we kept track of in above two iterations.

Note: We’re considering 0 as even number, as it has odd elements on either side of it. And 0 is perfectly divisible by 2.
scale

Explanation With Example

If a[5] = {10, 11, 12, 13, 14};
For Even Numbers

    even = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }
ia[i]a[i] % 2 == 0ResultDisplayCount
0a[0] = 1010 % 2TRUE101
1a[1] = 1111 % 2FALSE
2a[2] = 1212 % 2TRUE122
3a[3] = 1313 % 2FALSE
4a[4] = 1414 % 2TRUE143

Even numbers displayed:
10
12
14
Count: 3

For Odd Numbers

    odd = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }
ia[i]a[i] % 2 != 0ResultDisplayCount
0a[0] = 1010 % 2FALSE
1a[1] = 1111 % 2TRUE111
2a[2] = 1212 % 2FALSE
3a[3] = 1313 % 2TRUE132
4a[4] = 1414 % 2FALSE

Odd numbers displayed:
11
13
Count: 2

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

C Program To Delete Element of An Array at Specified Position

Write a C program to delete element of an array at user specified position. Show a confirmation message before deleting the element from specified position.

Related Read:
C Program To Shift Elements of An Array by n Position

Example: Expected Input/Output

Enter 5 integer numbers
10
12
14
16
18

Enter the position of the element to be deleted
2

You want to delete element 14 at position 2?
Yes: 1, No: 0
1

Array after deleting the specified element …
10
12
16
18

Visual Representation

delete element of an array at specified position

Video Tutorial: C Program To Delete Element of An Array at Specified Position


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

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

Source Code: C Program To Delete Element of An Array at Specified Position

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, pos, flag = 0;

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

    do
    {
        printf("\nEnter the position of the element to be deleted\n");
        scanf("%d", &pos);

        if(pos >= N)
            printf("\nPlease enter position within the range/size of the array\n");
        else
        {
            printf("\nYou want to delete element %d at position %d?\n", a[pos], pos);
            printf("Yes: 1, No: 0\n");
            scanf("%d", &flag);
        }
    }while(flag == 0);

    for(i = pos; i < (N - 1); i++)
        a[i] = a[i + 1];

    printf("\nArray after deleting the specified element ...\n");
    for(i = 0; i < (N - 1); i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
9
7
3

Enter the position of the element to be deleted
4

You want to delete element 3 at position 4?
Yes: 1, No: 0
0

Enter the position of the element to be deleted
2

You want to delete element 9 at position 2?
Yes: 1, No: 0
1

Array after deleting the specified element …
1
5
7
3

Output 2:
Enter 5 integer numbers
1
2
3
4
5

Enter the position of the element to be deleted
15

Please enter position within the range/size of the array

Enter the position of the element to be deleted
0

You want to delete element 1 at position 0?
Yes: 1, No: 0
1

Array after deleting the specified element …
2
3
4
5

Logic To Delete Element of An Array at Specified Position

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we ask the index position of the element which has to be deleted from the array. Once we’ve these inputs from the user, we show a confirmation message to the user before deleting the specified element. Here user can either choose “1” to delete the element or choose “0” to re-select the element to be deleted.

If user selects “1” and chooses to delete the selected element, then we initialize i to the position of the element to be deleted. We iterate the for loop until i is less than (N – 1), and for each iteration we increment the value of i by 1.

Note: Observe the condition i < (N – 1), which is similar to writing i <= (N – 2). That is because we don’t want to swap/insert the last element of the array with some garbage value present outside the array limit/size.

Inside the for loop
Inside the ‘for loop’ we assign the element present in the next index to the current index element selected by i. i.e,. a[i] is assigned the value present at a[i + 1]. This way the value or the element present at the user selected position is lost, and the index N – 1 and N – 2 will have same elements. While displaying the array elements we leave the last index element, indicating deletion of 1 element from the array – so the array size has been obviously reduced by 1.

Note: Here array variable size isn’t reduced, but we simply do not display the last element of the array and create the illusion of reducing the size of array by 1.

Explanation With Example

If a[5] = {10, 12, 14, 16, 18};
Delete element/number at position: 2
Element at user specified position: 14

    for(i = pos; i < (N - 1); i++)
        a[i] = a[i + 1];

We initialize i to user specified position, which is present in variable pos. Iterate this for loop until i in less than (N – 1), for each iteration increment the value of i by 1.

iposa[i]a[i + 1]a[i] = a[i + 1]
22a[2] = 14a[3] = 16a[2] = 16
32a[3] = 16a[4] = 18a[3] = 18
42a[4] = 18

Now that index i is 4 and the array size is N = 5. So 4 < (5 – 1) is 4 < 4 which returns false, so the control exits the for loop. Using another “for loop” we display the array elements from index 0 to N – 2. i.e., from index i to i < (N – 1) or i <= (N – 2).

So the array elements after execution of above logic:
a[4] = {10, 12, 16, 18};

That’s how we successfully delete a element/number from user specified position 2.

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