
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count the number of holes in an integer in C++
Given an array of holes[10] containing a number of holes in numbers 0 to 9. The goal is to find the number of holes in an integer number given as input. Given − holes[] = { 2, 1, 1, 0, 0, 1, 1, 1, 0 }
For Example
Input
number = 239143
Output
Count the number of holes in an integer are: 3
Explanation
We will count holes given in holes[] 239143 ( 1+0+0+2+0+0 )
Input
number = 12345
Output
Count the number of holes in an integer are: 3
Explanation
We will count holes given in holes[] 12345 ( 1+1+0+0+1)
Approach used in the below program is as follows −
Simply take each leftmost digit using num%10 and add hole count from holes[num%10] to count. Then reduce number num by 10.
Take an integer as input.
Initialize holes[] array.
Function holes_integer(int number, int holes[]) returns the count of number of holes in an integer.
Take the initial count as 0.
Traverse till number > 0.
Take leftmost digit as temp = number % 10. Add holes[temp] to count.
Reduce the number by 10.
Return count as result at the end.
Example
#include <bits/stdc++.h> using namespace std; int holes_integer(int number, int holes[]){ int count = 0; while (number > 0){ int temp = number % 10; count = count + holes[temp]; number = number/10; } return count; } int main(){ int number = 239143; int holes[] = { 2, 1, 1, 0, 0, 1, 1, 1, 0}; cout<<"Count the number of holes in an integer are: "<<holes_integer(number, holes); return 0; }
Output
If we run the above code it will generate the following output −
Count the number of holes in an integer are: 2
- Related Articles
- Java Program to Count Number of Digits in an Integer
- Swift Program to Count Number of Digits in an Integer
- Haskell Program to Count Number of Digits in an Integer
- Kotlin Program to Count Number of Digits in an Integer
- Java program to Count the number of digits in a given integer
- Count set bits in an integer in C++
- Golang Program to count the set bits in an integer.
- Count even and odd digits in an Integer in C++
- Python Program to Count set bits in an integer
- Java Program to Count set bits in an integer
- C/C++ Program to the Count set bits in an integer?
- Count the number of items in an array in MongoDB?
- Shift the bits of an integer to the right and set the count of shifts as an array with signed integer type in Numpy
- C/C++ Program to Count set bits in an integer?
- Count the number of data types in an array - JavaScript
