- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the frequency of a digit in a number using C++.
Here we will see how to get the frequency of a digit in a number. Suppose a number is like 12452321, the digit D = 2, then the frequency is 3.
To solve this problem, we take the last digit from the number, then check whether this is equal to d or not, if so then increase the counter, then reduce the number by dividing the number by 10. This process will be continued until the number is exhausted.
Example
#include<iostream> using namespace std; int countDigitInNum(long long number, int d) { int count = 0; while(number){ if((number % 10) == d) count++; number /= 10; } return count; } int main () { long long num = 12452321; int d = 2; cout << "Frequency of " << 2 << " in " << num << " is: " << countDigitInNum(num, d); }
Output
Frequency of 2 in 12452321 is: 3
- Related Articles
- Find the frequency of a number in an array using C++.
- C program to find frequency of each digit in a string
- Find the sum of first and last digit for a number using C language
- C++ program to find first digit in factorial of a number
- C++ Program to find the smallest digit in a given number
- C program to find sum of digits of a five digit number
- First digit in factorial of a number in C++
- Squaring every digit of a number using split() in JavaScript
- Program to find super digit of a number in Python
- Find the Number of Primes In A Subarray using C++
- Find the Number which contain the digit d in C++
- Find minimum possible digit sum after adding a number d in C++
- The units digit of a two digit number is 3 and seven times the sum of the digits is the number itself. Find the number.
- Find the Number of Substrings of a String using C++
- C++ Program to Find the Frequency of a Character in a String

Advertisements