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
Chitra
Chitra

5 thoughts on “Program to check the occurence of digit in a given number

  1. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in