Count the number of holes in an integer - GeeksforGeeks

29 Apr, 2021
Given an integer num, the task is to count the number of holes in that number. The holes in each digit are given below:  
Examples:  
Input: num = 6457819 Output: 5Input: num = 2537312 Output: 0  
 
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Global array for hole valuesint hole[] = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 }; // Function to return the count// of holes in numint 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 codeint main(){    int num = 6457819;     cout << countHoles(num);     return 0;}
// Java implementation of the approachimport java.io.*; class GFG{ // Global array for hole valuesstatic int hole[] = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 }; // Function to return the count// of holes in numstatic 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 codepublic static void main (String[] args){    int num = 6457819;    System.out.println(countHoles(num));}} // This code is contributed by// shk
# Python3 implementation of the approach # Global array for hole valueshole = [ 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 ] # Function to return the count# of holes in numdef 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 codenum = 6457819print(countHoles(num)) # This code is contributed by ihritik
// C# implementation of the approachusing 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// PHP implementation of the approach // Global array for hole values$hole = array(1, 0, 0, 0, 1, 0, 1, 0, 2, 1); // Function to return the count// of holes in numfunction countHoles($num){    global $hole;    $holes = 0;     while ($num > 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?>
<script> // Javascript implementation of the approach  // Global array for hole valueslet hole = [ 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 ]; // Function to return the count// of holes in numfunction countHoles( num){    let holes = 0;     while (num > 0) {         // Last digit in num        let d = num % 10;         // Update holes        holes += hole[d];         // Remove last digit        num = Math.floor(num/10);    }     // Return the count of holes    // in the original num    return holes;}      // Driver Code         let num = 6457819;     document.write(countHoles(num));     </script>
5
 
Shashank12
ankthon
ihritik
Akanksha_Rai
jana_sayantan
Infosys
number-digits
School Programming
Infosys
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Operator Overloading in C++
Polymorphism in C++
Copy Constructor in C++
Friend class and function in C++
Types of Operating Systems
Introduction To PYTHON
Exceptions in Java
Overriding in Java
Constructors in Java