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

Leave a Reply

Your email address will not be published. Required fields are marked *