C program to find the frequency of occurrence of digit in the given number


#include <stdio.h>

#include <stdlib.h>

int main()

{
    long num;

    int digit,rem,count=0;

    printf("Enter the Number: ");

    scanf("%ld",&num);

    printf("Enter the digit to be counted:");

    scanf("%d",&digit);

    while(num!=0)
    {

       rem=num%10;

       if(rem==digit)
        
       count++;
       
       num=num/10;
    }

printf("The digit %d present %d times ",digit,count);

    }


Comments

  1. IF i have a modified version of it, then can u code?

    instead of asking from the user which no to check for occurrence, cant u design the code to check for itself the digits and is occurrences.

    eg. 12223
    shall output 1- 1time
    2- 3 times
    3-1 time

    ReplyDelete
    Replies
    1. Try this code :)

      // C program to find the occurence of the number
      #include
      #include

      int main()
      {
      int a[10];
      int number,num,i,temp=0;
      printf("Enter the number");
      scanf("%d",&number);
      for(i=0;i<10;i++)
      {
      a[i]=0;
      }
      num=number;
      while (num>0)
      {temp=num%10;
      num=num/10;
      a[temp]++;
      }
      for(i=0;i<10;i++)
      {
      if (a[i]>0)
      printf("%d present %d times\n",i,a[i]);
      }



      return 0;
      }

      Delete
  2. // C program to find the occurence of the number
    #include
    #include

    int main()
    {
    int a[10];
    int number,num,i,temp=0;
    printf("Enter the number");
    scanf("%d",&number);
    for(i=0;i<10;i++)
    {
    a[i]=0;
    }
    num=number;
    while (num>0)
    {temp=num%10;
    num=num/10;
    a[temp]++;
    }
    for(i=0;i<10;i++)
    {
    if (a[i]>0)
    printf("%d present %d times\n",i,a[i]);
    }



    return 0;
    }

    ReplyDelete
  3. main()
    int num,rem,I;
    int d[10]={0};
    printf ("Enter a no ");
    scanf ("%d",&num);
    for(i=num;i!=0;i++)
    {
    rem=i%10;
    d[rem]++;
    }
    for(i=0;i<10;i++)
    printf ("%d occurs %d times\n",i,d[i]);
    }

    ReplyDelete

Post a Comment