Friday, May 17, 2013

Count digit repetition in number range

Q. Write a C program to count the no. of occurences of n's in a given number range.

For example:
random numbers : 45,78,2,65,489,6,2,6,55,9
find out how many times the digit 2 repeat?
2's digit repeat 2 times.

Ans.

/*c program to count the no. of occurences of n's in numbers range*/
#include<stdio.h>
int main()
{
 int arr[10],i,num,r,s,c=0;
 for(i=1; i<=10; i++)
 {
  printf("Enter %d number : ",i);
  scanf("%d"&arr[i]);
 }
 printf("\nEnter search number : ");
 scanf("%d", &s);

 for(i=1; i<=10; i++)
 {
  num = arr[i];
  while(num>=1)
  {
   r=num%10;
   if(r==s)
     c++;
   num=num/10;
  }
 }
 printf("\nThe digit %d is repeat in entered number is %d times",s,c);
 getch();
 return 0;
}

The output of above program would be:


Output of count repeate digit  in number range C program
Figure: Screen shot for count repeate digit
 in number range C program

No comments:

Post a Comment