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 Count Number of Positive, Negative and Zeros In An Array

Lets write a C program to count number of positive, negative and zeros in an Array.

Logic

number scale
If input number is greater than 0, then its positive number. If input number is less than 0, then its negative. If its neither greater than 0, nor less than 0, then the input number must be 0.

Related Read:
Number is Positive or Negative or Zero: C Program

Example: Expected Input/Output

Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Visual Representation

count positive negative zero in an array

Video Tutorial: C Program To Count Number of Positive, Negative and Zeros In An Array


[youtube https://www.youtube.com/watch?v=Z1mES-0NM-E]

YouTube Link: https://www.youtube.com/watch?v=Z1mES-0NM-E [Watch the Video In Full Screen.]

Source Code: C Program To Count Number of Positive, Negative and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

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

    for(i = 0; i < N; i++)
    {
        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Logic To Find Number of Positive, Negative and Zeros In An Array

First we initialize 0 to variables p, n and z – which represents positive, negative and zero. We accept 10 integer numbers from the user. Next we iterate through the array using for loop and check if the fetched array element is greater than 0, which means its a positive number, so we increment the value of variable p by one. If fetched array element is less than 0, which means its a negative number, so we increment the value of variable n by one. If both the cases fail, then the number must be zero, so we increment the value of z by 1.

After the completion of for loop execution, variables p, n and z will have the number of positive, negative and zeros present in the array.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

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

        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
0
-1
-2
-3
-4
-5

Positive no: 4
Negative no: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is greater than 0 or less than zero or is equal to zero, and increment the values of variable p, n and z accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. Less overhead and more efficient.

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 Number of Even, Odd and Zeros In An Array

Lets write a C program to count number of even, odd and zeros in an array.

What are Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Related Read:
Even or Odd Number: C Program

Example: Expected Output

Enter 10 integer numbers
10
15
0
-1
5
20
21
55
69
40

Even Numbers: 3
Odd Numbers: 6
Zeros: 1

Visual Representation

count even odd zero in an array

Video Tutorial: C Program To Count Number of Even, Odd and Zeros In An Array


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

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

Source Code: C Program To Count Number of Even, Odd and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

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

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

    for(i = 0; i < N; i++)
    {
        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven Numbers: %d\nOdd Numbers: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

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

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

Logic To Find Number of Even, Odd and Zeros In An Array

First we ask the user to enter N integer numbers. After that we iterate through the array elements one by one (using a for loop) and check if the fetched number is 0, if true, we’ll increment the value of zero by one. If the fetched element is not zero, then we check if its perfectly divisible by 2, if so, then its even number orelse its odd number – and we increment the values of variable even and odd accordingly.

Note: Make sure to first check if the fetched number is 0. Only after checking this, go further and check for even or odd conditions.

Method 2

#include<stdio.h>

#define N 10

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

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

        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven No: %d\nOdd No: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

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

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is equal to zero or is perfectly divisible by 2 or it is not perfectly divisible by 2. Based on that we increment the values of variable zero, even and odd accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. So less overhead and more efficient.

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 Each Digit In A Number using Arrays

Lets write a C program to count repetition of each digit in a positive integer number using array.

Related Read:
C Program To Check Repetition of Digit In A Number using Arrays

Example: Expected Output

Enter a positive number
11201

0 has appeared 1 times.
1 has appeared 3 times.
2 has appeared 1 times.

Digit Count Using Array

Video Tutorial: C Program To Count Each Digit In A Number using Arrays


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

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

Source Code: C Program To Count Each Digit In A Number using Arrays

Method 1

#include<stdio.h>

int main()
{
    int a[10] = {0}, num, rem, i;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }

    printf("\n");

    for(i = 0; i < 10; i++)
    {
        if(a[i] != 0)
            printf("%d has appeared %d times.\n", i, a[i]);
    }

    return 0;
}

Output 1:
Enter a positive number
1105135

0 has appeared 1 times.
1 has appeared 3 times.
3 has appeared 1 times.
5 has appeared 2 times.

Output 2:
Enter a positive number
12345

1 has appeared 1 times.
2 has appeared 1 times.
3 has appeared 1 times.
4 has appeared 1 times.
5 has appeared 1 times.

Logic To Count Each Digit In A Number

We ask the user to enter a positive integer number. We then fetch individual digits from the number using while loop.

Important Note:
1. Initially all the array elements are initialized to zero.
2. We take array size as 10. Because we need to accommodate 10 digits i.e., 0 to 9. Using digits 0 to 9 we could formulate any positive integer number.

Array with zeros initialized

Inside While loop
While loop keeps executing until num is 0. Once num is 0, control exits while loop. First we fetch the last digit of the user input number by modulo dividing it by 10, and store it inside variable rem. Next we use this rem value as index of array variable a. i.e., a[rem] and add 1 to the previous value of a[rem]. Next we reduce the user input number by 1 digit from the end by dividing the number by 10. i.e., num = num / 10. This line of code shifts the decimal point from right to left by 1 place. But since num is integer variable and 10 is also integer(by which we divide user input number), we only get the integer part of the number and the number after decimal point gets discarded.

Printing / Displaying The Result or The Count
We iterate through the entire array and display the non-zero values along with the index number. Index number is nothing but the individual digits of the user input number.

Explanation With Example

If num = 112021;
So individual digits are 1, 1, 2, 0, 2, 1

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }
num = num / 10rem = num % 10a[rem] = a[rem] + 1
1120211a[1] = 1
112022a[2] = 1
11200a[0] = 1
1122a[2] = 2
111a[1] = 2
11a[1] = 3
0

When the control exits while loop (once num is 0), a[0] has value 1, a[1] has value 3, a[2] has value 2. That simply means, digit 0 has appeared 1 time in the user input number. Digit 1 has appeared 3 times in the user input number. Digit 2 has appeared 2 times in the user input number.

Source Code: C Program To Count Each Digit In A Number using Arrays

Another method: Method 2

#include<stdio.h>

int main()
{
    int a[10] = {0}, num, rem, temp;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }

    printf("\n");

    while(temp)
    {
        rem = temp % 10;

        if(a[rem] != 0)
            printf("%d appeared %d time.\n", rem, a[rem]);

        a[rem] = 0;
        temp = temp / 10;
    }

    return 0;
}

Output 1:
Enter a positive number
11253001

0 has appeared 2 times.
1 has appeared 3 times.
2 has appeared 1 times.
3 has appeared 1 times.
5 has appeared 1 times.

Output 2:
Enter a positive number
112021

0 has appeared 1 times.
1 has appeared 3 times.
2 has appeared 2 times.

Logic To Count Each Digit In A Number: Method 2

Here the program is same except the displaying part logic.

    while(temp)
    {
        rem = temp % 10;

        if(a[rem] != 0)
            printf("%d appeared %d time.\n", rem, a[rem]);

        a[rem] = 0;
        temp = temp / 10;
    }

variable temp has user input number. Here we fetch individual digit of the user input number, and if it is non-zero, then we print whatever value is present at that index. Once we print the value, we over-write the value at that index by 0. Next we reduce the number by one digit from left/end by dividing the number by 10.

This way we only iterate through the while loop by limited number of times. i.e., The number of iteration is equal to the number of unique digits the user input number has.

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 Prime Numbers Between Range

Lets write a C program to count prime numbers between user entered/input range of numbers, using function/method.

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Related Read:
C Program To Find Prime Number or Not using For Loop

Video Tutorial: C Program To Count Prime Numbers Between Range


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

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

Source Code: C Program To Count Prime Numbers Between Range

#include<stdio.h>
#include<math.h>

int isPrime(int num)
{
    int inum = sqrt(num), prime = 1, count;

    for(count = 2; count <= inum; count++)
    {
         if(num % count == 0)
         {
            prime = 0;
            break;
         }
    }

    return(prime);
}

int main()
{
    int start, end, temp, num, slno = 0, on_off;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("Do you want to print prime numbers? (yes = 1, no = 0)\n");
    scanf("%d", &on_off);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    if(on_off)
        printf("\nPrime numbers between %d and %d are:\n\n", start, end);

    for(num = start; num <= end; num++)
    {
        if(num == 1)
        {
            continue;
        }

        if( isPrime(num) )
        {
            slno++;

            if(on_off)
            {
                printf("%d. %d is prime number.\n", slno, num);
            }
         }
    }

    printf("\nThere are %d prime numbers between %d and %d.\n", 
             slno, start, end);

    return 0;
}

Output 1:
Enter start and end value
10
30
Do you want to print prime numbers? (yes = 1, no = 0)
1

Prime numbers between 10 and 30 are:

1. 11 is prime number.
2. 13 is prime number.
3. 17 is prime number.
4. 19 is prime number.
5. 23 is prime number.
6. 29 is prime number.

There are 6 prime numbers between 10 and 30.

Output 2:
Enter start and end value
10
30
Do you want to print prime numbers? (yes = 1, no = 0)
0

There are 6 prime numbers between 10 and 30.

Output 3:
Enter start and end value
1
10
Do you want to print prime numbers? (yes = 1, no = 0)
1

Prime numbers between 1 and 10 are:

1. 2 is prime number.
2. 3 is prime number.
3. 5 is prime number.
4. 7 is prime number.

There are 4 prime numbers between 1 and 10.

Output 4:
Enter start and end value
1
300
Do you want to print prime numbers? (yes = 1, no = 0)
0

There are 62 prime numbers between 1 and 300.

Output 5:
Enter start and end value
1
1000
Do you want to print prime numbers? (yes = 1, no = 0)
0

There are 168 prime numbers between 1 and 1000.

Logic To Count Prime Numbers Between Range

First we ask the user to enter the range and store it inside address of variables start and end. We make sure that start value is less than end value. If start value is greater than end value, we use a temporary variable to swap the values of start and end.

Swap 2 Numbers Using a Temporary Variable: C

Next we ask the user if he / she wants to display the prime numbers between the range or just want to know the count of prime numbers between the entered range. We store the user answer in a variable called on_off.

We start the for loop: We initialize the loop counter variable num to start and iterate through the loop until num is less than or equal to end. For each iteration of the for loop we increment the value of num by 1. This for loop selects number one by one from start to end. And this selected number, which is present inside variable num is checked for prime or not. If its prime we display it to the console window and keep track of the count of prime numbers, if not we simply ignore that non-prime number.

We use a function to check if the selected number present in variable num is a prime number or not. We call the function/method isPrime() inside if condition and pass the value of num to it. isPrime() returns 1 or 0 value. 1 means true and 0 means false. So if isPrime() returns 1, then the if condition becomes true and we increment the value of slno by 1 and optionally printout the prime number. If isPrime() returns 0, then we simply ignore it and go to the next number.

isPrime() function has the logic to determine if the given number is prime or not. We have explained the complete logic of this in a separate video tutorial, link to which is present below.

C Program To Find Prime Number or Not using For Loop

Note:
1. We are including math.h header file or library file since we are using sqrt() builtin method. sqrt() method is present inside math.h file.

2. We are also using continue and break keywords in our program and we’ve explained about it in detail in separate videos. Please watch them for more clarity about the topic.

Continue Statement In C Programming Language
break Statement In C Programming Language

Stay subscribed to our blog and YouTube channel. Thank you ..

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