Thread: Counting number of occurence of digits in a number

  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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The approach that I recommend is to create an array of counts, initialised to 0. Each index of the array corresponds to a digit, e.g., counts[0] would contain the number of times 0 has been counted thus far, counts[1] would contain the number of times 1 has been counted thus far, etc.

    Then, you break up the number input into individual digits, and increment the count value corresponding to that digit. When done, you loop over the array of counts and print the result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Quote Originally Posted by laserlight View Post
    The approach that I recommend is to create an array of counts, initialised to 0. Each index of the array corresponds to a digit, e.g., counts[0] would contain the number of times 0 has been counted thus far, counts[1] would contain the number of times 1 has been counted thus far, etc.

    Then, you break up the number input into individual digits, and increment the count value corresponding to that digit. When done, you loop over the array of counts and print the result.
    The challange is not to use arrays. Use modulo and division operation to calculate.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, use counting sort as laserlight suggests. digits[a_digit]++ kind of logic.

    No arrays? Hmmmm....

    Ten variables then?
    Last edited by Adak; 10-12-2012 at 11:02 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Azeem
    The challange is not to use arrays. Use modulo and division operation to calculate.
    You are mistaken: you have to store the counts somewhere, and in this case an array is an excellent way of doing so. The challenge that you speak of is likely one where you read the input as an integer rather than as a string.

    EDIT:
    Quote Originally Posted by Adak
    No arrays? Hmmmm....

    Ten variables then?
    That would be moronic since "modulo and division operation" would be useful to break up the number input into individual digits, but once you have an individual digit, those operations cannot tell you which of the ten variables to update. You would be either using a switch, a series of if/else statements, or... treat the digit as the index to an array of counts.
    Last edited by laserlight; 10-12-2012 at 11:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    I am storing the input in integer variable.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Azeem
    I am storing the input in integer variable.
    Yes, I think that the correct idea in this case. So, also create an array of ten integers to store the counts for each possible digit. You do understand what I described in my post #2, right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see how the i==rem and count++ logic, could work.

    Can you explain the algorithm for that part? Maybe you're trying to create a packed number, which would hold the answer?
    Last edited by Adak; 10-12-2012 at 11:17 PM.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Yes, I understand but instead of using count array can I use count variable and use it in a loop?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Azeem
    Yes, I understand but instead of using count array can I use count variable and use it in a loop?
    Why do you want to do that? The thing is, you need to track the counts for each digit. A single count variable will is not enough.

    (Well, okay, a single count variable is enough, if you repeat the whole process ten times, i.e., for each digit, you break the input number into individual digits and count the number of times it occurs in the input number. But in my opinion that is a rather silly approach when the memory consumption is so incredibly low.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Quote Originally Posted by Adak View Post
    I don't see how the i==rem and count++ logic, could work.

    Can you explain the algorithm for that part?
    i is a range of number from 1 to 10 and rem is remainder of entered digit. if statement compares whether i is equal to rem or not if i is equal to rem then count is incremented.

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    laserlight
    I dont want to use arrays in my program, I want alternative way.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Azeem
    I dont want to use arrays in my program, I want alternative way.
    In that case, be a silly programmer and use ten separate variables, each to keep track of the count for a single digit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    For each digit (in range 0 to 9), count the number of times that digit is present in the value, and print the count.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I can think of three ways to do it without arrays:

    1. You can use 10 variables.

    2. You can go through the number 10 times, once for each digit.

    3. You could store all the counts in a single integer by interpreting each decimal position as a count. This limits the max count of any digit to 9. This could be increased by using another base.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

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