Occurrence of a digit in a given number using C++

Occurrence of a digit in a given number in C

Occurrence of a digit in a given number using C++

Here, in this page we will discuss the program that count the occurrence of a digit in a given number using C++.
The input may lie within the range of integer.

Method Discussed

  • Method 1 : Iterative way
  • Method 2 : Recursive way.

Let’s discuss the working and algorithm of both methods in brief.

Method 1

  • Declare variable count that will count the required number of occurrences
  • Take a while loop.
  • Declare a variable rem to store every digit of the number to be compared.
  • Compare rem with the digit
    • if rem equals digit increment count.
  • n=n/10
  • Print the value of count.

Method 1 : Code in C++

Run
//Write a program to print the Occurrence of a Digit in a given Number in C++
#include <iostream>
using namespace std;
int main() {

    int n = 890190798; 
    int d = 9; 

    int count=0; 

    while(n) {

        int rem = n%10; 
        if(rem == d){
            count++;
        }
        n=n/10; 
    }

    cout<<count;

    return 0;

}

Output:

3

Method 2 :

In this method we will implement the recursive code for the algorithm discuss in method 1.

Method 2 : Code in C++

Run
//Write a program to print the Occurrence of a Digit in a given Number in C++
#include <iostream>
using namespace std;

int count(int n, int d){
    if(n<=0)
    return 0;
    
    int rem = n%10;
    
    if(rem == d){
        return 1 + count(n/10, d);
    }
    
    return count(n/10, d);
}
int main() {

    int n = 890190798; 
    int d = 9; 
    
    int x = count(n, d);
    cout<<x;

    return 0;

}


Output:

3

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Occurrence of a digit in a given number using C++”


  • bhagatnishant5454

    #include
    using namespace std;
    int main(){
    int n,k=0;
    cin>>n;
    int arr[n];
    for(int i=0;i>arr[i];
    }
    if(arr[i]==given input){
    k++;
    }
    cout<<"No of times"<<k<<" "<<endl;
    return 0;
    }


  • Khush

    using for loop:
    #include
    using namespace std;
    int main()
    {
    long nm, digit;
    cout <> nm;
    cout <> digit;
    //–>Finding digit occurance
    //->Separating digit and counting:
    int count = 0;
    for (int i = nm; i > 0; i /= 10)
    {
    int rem = i % 10;
    if (digit == rem)
    {
    count++;
    }
    }
    cout << "Digit " << digit << " occured in the number: " << count << " times.";
    return 0;
    }


  • Shubham

    public class JavaApplication1
    {
    public static void main(String[] args)
    {
    Scanner obj = new Scanner(System.in);
    int x,c=0;
    x = obj.nextInt();
    char b = obj.next().charAt(0);
    String s = Integer.toString(x);
    for(int i=0;i<s.length();i++)
    {
    if(s.charAt(i)==b)
    c=c+1;
    }
    System.out.println(c);
    }
    }