Thread: Check for repeated digits!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Check for repeated digits!

    OK this code works to display one of the numbers that would be repeated. However I need to display all the numbers that may be repeated. Would I need to incorporate a printf function somewhere in the loop?

    Code:
    #include <stdio.h>
    
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    main ()
    {
    	Bool digit_seen[10] = {0};
    	int digit = 0;
    	int display_digit = 0;
    	long int n = 0;
    
    	printf("Enter a number: ");
    	scanf("%ld", &n);
    
    	while (n > 0) 
    	{
    		digit = n % 10;
    		if (digit_seen[digit])
    		{
    			display_digit = digit;
    			break;
    		}
    		digit_seen[digit] = TRUE;
    		n /= 10;
    	}
    
    	if (n > 0)
    		printf ("Digits repeated are: %ld\n\n", display_digit);
    	else
    		printf("No repeated digit\n\n");
    
    	return 0;
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Would I need to incorporate a printf function somewhere in the loop?
    Yes, that would be one way to do it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Instead of having a boolean array, have an integer array, then for each time a digit is encountered, just increment that integer in the array.

    eg.
    Code:
    int digit_seen[10] = {0};
    int digit, n;
    
    printf("Enter a number: ");
    scanf("%ld", &n);
    
    while (n > 0) 
    {
        digit = n % 10;
        digit_seen[digit]++;
        n /= 10;
    }
    This way, you can do the printing after the loop.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by The Dog
    Instead of having a boolean array, have an integer array, then for each time a digit is encountered, just increment that integer in the array.

    eg.
    Code:
    int digit_seen[10] = {0};
    int digit, n;
    
    printf("Enter a number: ");
    scanf("%ld", &n);
    
    while (n > 0) 
    {
        digit = n % 10;
        digit_seen[digit]++;
        n /= 10;
    }
    This way, you can do the printing after the loop.
    OK...I can see how you changed it. However, it still gives out only one printout. If there are more repeated numbers then they are over written. Is there suppose to be something in the loop to catch them for a check?

    CJ

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> Is there suppose to be something in the loop to catch them for a check?<<
    The digit_seen array holds a count of how many times each digit has been seen. Simply loop through this array, printing all entries with a count greater than 1.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Thank you guys!! You have no idea how much you helped me out! That last phrase by hammer (even though a DUH) made me realize a concept of arrays that I was missing.

    CJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  3. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  4. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM