# Number Of Times X Digit Occurs In Each And Every Number From 0 To N C++ code

## Write a C program to find Number of times x digit occurs in each and every number from 0 to n:-

A common solution to find the number of times x digit occurs in each and every number from 0 to n. Here, the x number is taken as 3 where the program will calculate the number of 3s occurring from 0 to n. For every number being visited, the program will calculate the number of 3s in it. ## Algorithm

• Step 1: Start
• Step 2: Ask the user to insert the number n as an input.
• Step 3: Initialize count = 0
• Step 4: Repeat all the numbers from 0 to n.
• Step 5: Find if num % 10 == 3
• Step 6: Increase count
• Step 7: Divide the num by 10
• Step 8: Return count
• Step 9: Stop

### C++ Code:-

`#include <iostream>using namespace std;int count_3s(int n){int counter = 0;while (n > 0){//for find number of digitif (n % 10 == 3){counter++;}// n/=10;n = n / 10;}return counter;}int count_in_range(int n){int counter = 0 ;for (int i = 2; i <= n; i++){counter = counter + count_3s(i);}return counter;}int main(){ //initialize of n variablesint n;cout<<"\nInsert the end value : ";cin>>n;cout<<"\nTotal occurrences of 3 from 0 to "<<n<<" is "<<count_in_range(n);return 0;}`
`Insert the end value : 40Total occurrences of 3 from 0 to 40 is 14`