Counting quantity of different digits

This is a discussion on Counting quantity of different digits within the C Programming forums, part of the General Programming Boards category; What I'm doing wrong . . . I need to count quantity of different digits in number for example 321123 ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    15

    Counting quantity of different digits

    What I'm doing wrong . . .
    I need to count quantity of different digits in number
    for example 321123 : and output i want like that

    There r 3 different digits in the number 321123


    Code:
     while(n)
     
    	   {  dif=n%10;
    	   n/=10;
    	   if (dif=n%10)
    		   n/=10;
    	   else
    	   count++;
    	   }
    	   printf("\nThere r  ",count);
    	   printf(" different digits in the number",n);

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    
    int main() {
    	int n=321123, copy = n, r, counts[10] = {0}, digits = 0;
    	while (copy) {
    		r = copy%10;
    		counts[r]++;
    		copy /= 10;
    	}
    
    	for (r=0;r<10;r++) {
    		if (counts[r]) {
    			printf("%d %d's\n",counts[r],r);
    			digits++;
    		}
    	}
    
    	printf("%d contains %d different digits.\n",n,digits);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 02:13 PM
  2. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 02:30 PM
  3. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 01:55 PM
  4. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 12:39 PM
  5. Counting integer digits
    By Lionmane in forum C Programming
    Replies: 22
    Last Post: 05-24-2005, 11:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21