The C++ program to count the occurrence of a digit in a number is a simple program that receives two inputs – a number and a digit

It counts the number of times a digit appears in the given number and prints the output. The occurrence is 0 when the digit is not in the number.

This program is intended for beginners of C++ programming language. We used Dev-C++ compiler – version 4.9.9.2 installed on a Windows 7 64-bit computer. This program code will work with any standard C++ compiler if the correct header files are used.

Read: C Program to Count Frequency of Vowels

You will find the following section to help you understand the program logic.

- Problem Definition
- Flowchart of the program.
- Program Source Code
- Verified Output after running the program.

Contents

## Problem Definition

The program need two inputs as follows

- A positive integer number
- A positive digit

If you do not use positive value the program fails, so make sure that the inputs are always positive number.

We use 4 steps to solve the problem of counting the occurrence of a digit. The occurrence is stored in a variable called **count**.

- When the number is given, each digit must be separated.
*Extracted digit*is compared with the input digit.- If there is a match – (
*digit == extracted digit*), then count is incremented by 1, otherwise check the next extracted digit, until all the digits in the given number is exhausted. - Print the counted value as output.

## Flowchart – Count the Occurrence of a Digit

A flowchart of the program show that we make two comparisons during the execution of the program.

- Check if all the digits in the given number is checked – (
**while (quotient! = 0)).** - Check if extracted digits from the number is equal to input digit so that we can count the occurrence –
**if (digit == remainder).**

## Program Code – Count the Occurrence of a Digit

```
/* C++ Program to Count the Occurrence of a Digit in a Number */
#include < cstdlib >
#include < iostream >
#include < conio.h >
using namespace std;
int main()
{
int number, remainder, quotient;
int count, digit, i;
count = digit = i = 0;
cout << "\n\n\n\n";
cout << "\t\t\tEnter the number:"; cin >> number;
cout << "\t\t\tEnter the digit:"; cin >> digit;
quotient = number;
while (quotient! = 0)
{
remainder = quotient % 10;
quotient = quotient / 10;
if (digit == remainder)
{
count = count + 1;
}
}
cout << "\n\n\n\n";
cout << "\t\t\tThe digit" << " "
<< digit << " " << "appeared" << " "
<< count << " " << "times\n\n";
getch();
return EXIT_SUCCESS;
}
```

We use two important logic in this program to count the occurrence of a digit.

`Remainder = quotient % 10;`

The above statement will store the remainder which is extracted digit we want.

For example,

123 % 10 will give a remainder of 3.

`Quotient = quotient / 10;`

The above statement will give next number from which we want to extract a digit.

For example,

123 % 10 will extract the digit 3, but next time we want to extract digit 2 which is only possible if we have number 12.

So if we divide 123 by 10, the quotient is 12 which is the number we want for next iteration to extract another digit.

## Output

The output of the program is given below.