# Count the number of holes in an integer

Given an integer num, the task is to count the number of holes in that number. The holes in each digit are given below:

Digit Number of Holes
0 1
1 0
2 0
3 0
4 1
5 0
6 1
7 0
8 2
9 1

Examples:

Input: num = 6457819
Output: 5

Input: num = 2537312
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize holes = 0 and an array hole[] with the values given where hole[i] stores the number of holes in the digit i. Now, for every digit d in num update holes = holes + hole[d]. Print the holes in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Global array for hole values ` `int` `hole[] = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 }; ` ` `  `// Function to return the count ` `// of holes in num ` `int` `countHoles(``int` `num) ` `{ ` `    ``int` `holes = 0; ` ` `  `    ``while` `(num > 0) { ` ` `  `        ``// Last digit in num ` `        ``int` `d = num % 10; ` ` `  `        ``// Update holes ` `        ``holes += hole[d]; ` ` `  `        ``// Remove last digit ` `        ``num /= 10; ` `    ``} ` ` `  `    ``// Return the count of holes ` `    ``// in the original num ` `    ``return` `holes; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `num = 6457819; ` ` `  `    ``cout << countHoles(num); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Global array for hole values ` `static` `int` `hole[] = { ``1``, ``0``, ``0``, ``0``, ``1``, ``0``, ``1``, ``0``, ``2``, ``1` `}; ` ` `  `// Function to return the count ` `// of holes in num ` `static` `int` `countHoles(``int` `num) ` `{ ` `    ``int` `holes = ``0``; ` ` `  `    ``while` `(num > ``0``)  ` `    ``{ ` ` `  `        ``// Last digit in num ` `        ``int` `d = num % ``10``; ` ` `  `        ``// Update holes ` `        ``holes += hole[d]; ` ` `  `        ``// Remove last digit ` `        ``num /= ``10``; ` `    ``} ` ` `  `    ``// Return the count of holes ` `    ``// in the original num ` `    ``return` `holes; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `num = ``6457819``; ` `    ``System.out.println(countHoles(num)); ` `} ` `} ` ` `  `// This code is contributed by  ` `// shk `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Global array for hole values  ` `hole ``=` `[ ``1``, ``0``, ``0``, ``0``, ``1``, ``0``, ``1``, ``0``, ``2``, ``1` `]  ` ` `  `# Function to return the count  ` `# of holes in num  ` `def` `countHoles(num):  ` ` `  `    ``holes ``=` `0` ` `  `    ``while` `(num > ``0``) : ` `     `  `        ``# Last digit in num  ` `        ``d ``=` `num ``%` `10` ` `  `        ``# Update holes  ` `        ``holes ``=` `holes ``+` `hole[d]  ` ` `  `        ``# Remove last digit  ` `        ``num ``=` `num ``/``/` `10` `     `  `    ``# Return the count of holes  ` `    ``# in the original num  ` `    ``return` `holes ` `     `  `# Driver code  ` `num ``=` `6457819` `print``(countHoles(num)) ` ` `  `# This code is contributed by ihritik `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Global array for hole values  ` `    ``static` `int` `[]hole = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 };  ` `     `  `    ``// Function to return the count  ` `    ``// of holes in num  ` `    ``static` `int` `countHoles(``int` `num)  ` `    ``{  ` `        ``int` `holes = 0;  ` `     `  `        ``while` `(num > 0)  ` `        ``{  ` `     `  `            ``// Last digit in num  ` `            ``int` `d = num % 10;  ` `     `  `            ``// Update holes  ` `            ``holes += hole[d];  ` `     `  `            ``// Remove last digit  ` `            ``num /= 10;  ` `        ``}  ` `     `  `        ``// Return the count of holes  ` `        ``// in the original num  ` `        ``return` `holes;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `num = 6457819;  ` `        ``Console.WriteLine(countHoles(num));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

## PHP

 ` 0) ` `    ``{ ` ` `  `        ``// Last digit in num ` `        ``\$d` `= ``\$num` `% 10; ` ` `  `        ``// Update holes ` `        ``\$holes` `+= ``\$hole``[``\$d``]; ` ` `  `        ``// Remove last digit ` `        ``\$num` `=  (int)(``\$num` `/ 10); ` `    ``} ` ` `  `    ``// Return the count of holes ` `    ``// in the original num ` `    ``return` `\$holes``; ` `} ` ` `  `// Driver code ` `\$num` `= 6457819; ` ` `  `echo` `countHoles(``\$num``); ` ` `  `// This code is contributed  ` `// by Akanksha Rai ` `?> `

Output:

```5
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.