Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C program to count number of times x digit occurs in each and every number from 0-n

Number of times x digit occurs in each number from 0 to n

 
In this C program, we will be counting the occurrence of a single digit in a number or from a group of digits. The number can be of any length starting from one to infinity, and Number can be ranging between -infinity to +infinity if we talk about numbers only. Other than these numbers can be divided into different groups like:
 
  • Whole Number
  • Natural Number
  • Rational Number
  • Real Number
 
Etc. In this case, we will be counting the occurrence of a digit in a number without being specific to its type and its range.
Python program to count number of times x digit occurs in each and every number from 0 to n

Algorithm

  • Step 1:- Start.
  • Step 2:- Take input from the user.
  • Step 3:- Take input of a digit whose occurrence is to be found.
  • Step 4:- Change the datatype of the integer inputs.
  • Step 5:- use the count function to count the occurrence of the digit and print it.
  • Step 6:- End.

C Program for occurrence of a Digit

#include <stdio.h>
int main()
{
    int number, digit;
    int count = 0;
    printf("Enter the number : ");
    scanf("%d",&number);
    printf("Enter the digit : ");
    scanf("%d",&digit);
    while(number!=0)
    {
        int rem = number%10;
        if(rem==digit)
        {
            count++;
        }
        number = number/10;
    }
    printf("Digit count is %d",count);
    return 0;
}
Output
Enter the Number : 897982
Enter the digit : 9
Digit count is 2