Program to check the occurence of digit in a given number

This program checks the occurrence of digit 5 in a given number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "conio.h"
#include "stdio.h"
 
void main()
{
  int num,i,n,k;
  printf("\nEnter a number : ");
  scanf("%d",&num);
  n=num;
  i=0;
  while(n!=0)
  {
  k=n%10;
  n=n/10;
  if(k==5)
  {
  i++;
  }
  }
  printf("\nThe occurrence of 5 is %d times",i);
getch();
}

Output:

Enter a number : 21353
The occurrence of 5 is 1 times

7 Responses to “Program to check the occurence of digit in a given number”

    • n%10 means simply it will give you the last digit of given number.

      Ex: 1%10=1;
      EX: 22%10= 2 , 2(if we keep this in loop)

      Reply
  1. Profile photo of Rasmi Ranjan Nayak

    /*Works for all n…. below Code not tested, but should work*/

    /*Previous code*/
    …………
    …………
    int num,i,n,k, digit/*digit added here*/;
    printf(“\nEnter a number : “);
    scanf(“%d”,&num);
    printf(“\n Enter the digit you would like to search for its repeatation”);
    scanf(“%d”, &digit);
    /*Above 2 lines added*/
    n=num;
    i=0;
    while(n!=0)
    {
    k=n%10;
    n=n/10;
    if(k==digit)/*comparing with entered digit number*/
    ………..
    …………
    /*Then rest of the code above follows*/

    ReplyReport user
  2. vaibhav

    I want to return a digit which occurs maximum time in a given number

    Reply
  3. I want a programme in which the position of even no. Will be displayed within the no. Given by the user

    Reply

Leave a Reply