Thread: Counting number of occurence of digits in a number

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    26

    Counting number of occurence of digits in a number

    I am writing a program in which user enter any value and the program calculates the occurence of any digit in a number. For example, if user enter 4323122 the output should like this

    3 is present 2 times in a number.
    2 is present 3 times in a number.


    The code I have written is here but there is a mistake which i am unable to resolve.
    Code:
    #include<stdio.h>
    int main(void)
    {
        int num, m, div=1, rem, count=0, i,n, a;
        printf("Enter Number: ");
        scanf("%d", &num);
        div=num;
        for(i=1; i<=10; i++)
        {
            div=num;
            while(div!=0)
            {
            rem=div%10;
            div=div/10;
    
    
            if(i==rem)
            {
                count++;
            }
    
    
            if(i==rem && count>=2)
            {
                printf("\n%d is present %d times", i, count);
            }
            }
        }
        return 0;
    }
    Last edited by Azeem; 10-12-2012 at 10:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the number of inegers in a number
    By jburn09 in forum C Programming
    Replies: 8
    Last Post: 09-19-2012, 12:52 PM
  2. Replies: 7
    Last Post: 12-04-2011, 10:43 PM
  3. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  4. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 01:30 PM
  5. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM