C Program To Count Digit k in Number n

Lets write a C program to count the number of occurrences of digit k in user input positive integer number n, using iterative logic. We will be using while loop to iterate in this program.

For Example:

If user inputs n = 1550, and k = 5, then our C program should find how many times digit 5 is present in number 1550.

Note: (num % 10) fetches last digit in num. If num = 1234; (num%10) fetches the last digit from right, which is 4.

(num/10) removes the last number from right. If num = 1234; (num/10) removes last digit i.e., 4 and the result of (num/10) will be 123.

Related Read:
C Program To Count Digit k in Number n using Recursion

Video Tutorial: C Program To Count Digit k in Number n


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

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

Source Code: C Program To Count Digit k in Number n

#include<stdio.h>

int occurrence(int, int);

int main()
{
    int n, k;

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

    printf("Which digits occurrence you want to check for?\n");
    scanf("%d", &k);

    printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

    return 0;
}

int occurrence(int num, int k)
{
    int count = 0;

    while(num)
    {
        if(num%10 == k)
            count++;

        num = num / 10;
    }
    return count;
}

Output:
Enter a positive integer number
1550
Which digits occurrence you want to check for?
5

5 has appeared 2 times in 1550.

Logic To Count Digit k in Number n

We ask the user to input values for variable n and k. We need to find the number of occurrences of digit k in number n. We pass both these variables to a function called occurrence().

Inside occurrence() function
Inside occurrence() function, we keep iterating the while loop until num value is 0. Btw, n value is copied into variable num. Now we check if (num%10) is equal to value of k. (num%10) fetches the last digit in the num. If (num%10) is equal to k, then we increment the value of count by 1. For each iteration of while loop we decrement the value of num by 1 digit, from right, by dividing num by 10 i.e., (num/10).

Once num is 0, control exits while loop and value of count will be returned to main method.

If num%10 is not equal to k, then we simply divide the num by 10, and reduce the value of num by one digit from right.

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 Digit k in Number n using Recursion

Lets write a C program to count the number of occurrences of digit k in user input positive integer number n, using recursion.

For Example:

If user inputs n = 12235, and k = 2, then our C program should find how many times digit 2 is present in number 12235.

Note: (num % 10) fetches last digit in num. If num = 1234; (num%10) fetches the last digit from right, which is 4.

(num/10) removes the last number from right. If num = 1234; (num/10) removes last digit i.e., 4 and the result of (num/10) will be 123.

Related Read:
C Program To Count Digit k in Number n

Video Tutorial: C Program To Count Digit k in Number n using Recursion


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

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

Source Code: C Program To Count Digit k in Number n using Recursion

#include<stdio.h>

int occurrence(int, int);

int main()
{
    int n, k;

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

    printf("Which digits occurrence you want to check for?\n");
    scanf("%d", &k);

    printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

    return 0;
}

int occurrence(int num, int k)
{
    if(num == 0)
        return 0;
    else if(k == (num%10))
        return(1 + occurrence(num/10, k));
    else
        return(occurrence(num/10, k));
}

Output:
Enter a positive integer number
12555
Which digits occurrence you want to check for?
5

5 has appeared 3 times in 12555.

Source Code: C Program To Count Digit k in Number n using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int occurrence(int, int);

int main()
{
    int n, k;

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

    printf("Which digits occurrence you want to check for?\n");
    scanf("%d", &k);

    printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

    return 0;
}

int occurrence(int num, int k)
{
    return( (num == 0)? 0 :
            (k == (num%10)) ?
            (1 + occurrence(num/10, k)) :
            (occurrence(num/10, k)));
}

Output:
Enter a positive integer number
155555061
Which digits occurrence you want to check for?
5

5 has appeared 5 times in 155555061.

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C.

Logic To Count Digit k in Number n using Recursion

First we write the base condition i.e., if num is 0, then our function returns 0 to the calling function. Else if num%10 is equal to the value of k, then we return (1 + occurrence(num/10, k)). That way we count or increment by 1 for each time we found a digit which is equal to k, and then we reduce the num by one digit from right by doing num/10. k value will remain same throughout the program execution.

If num%10 is not equal to k, then we simply divide the num by 10, and reduce the value of num by one digit from right.

Example:

Lets assume that user input value for n and k:
n = 1223; k = 2;

n(n%10) == kcountn/10
1223False122
122True112
12True1+1=21
1False1+1=20

So, 2 appeared 2 times in 1223.

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