# Counting number of occurence of digits in a number

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-12-2012
Azeem
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; }```
• 10-12-2012
laserlight
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.
• 10-12-2012
Azeem
Quote:

Originally Posted by laserlight
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.
• 10-12-2012
Yes, use counting sort as laserlight suggests. digits[a_digit]++ kind of logic.

No arrays? Hmmmm....

Ten variables then?
• 10-12-2012
laserlight
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:

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.
• 10-12-2012
Azeem
I am storing the input in integer variable.
• 10-12-2012
laserlight
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?
• 10-12-2012
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?
• 10-12-2012
Azeem
Yes, I understand but instead of using count array can I use count variable and use it in a loop?
• 10-12-2012
laserlight
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.)
• 10-12-2012
Azeem
Quote:

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.
• 10-12-2012
Azeem
laserlight
I dont want to use arrays in my program, I want alternative way.
• 10-12-2012
laserlight
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.
• 10-12-2012
grumpy
For each digit (in range 0 to 9), count the number of times that digit is present in the value, and print the count.
• 10-12-2012
oogabooga
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last