Thread: How to find repeated digits in a number

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    How to find repeated digits in a number

    This is my solution, but the Output is as follows:-
    Contains Duplicate Number:7 Count 2
    Contains Duplicate Number:7 Count 3
    Is there a way i can get only 1 Output like this :-
    Contains Duplicate Number:7 Count 3
    Any Help Appreciated


    Code:
        int num =677827,i=0,rem=0;
        int seen[10]={0};
    
    
        while(num >0) {
            rem = num %10;
            seen[rem] ++;
            num/=10;
            if((seen[rem]) >1 ) {
                printf("\nContains Duplicate Number:%d  Count : %d",rem,seen[rem]);
            }
        }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,651
    You have one loop to do the counting (as you are now).

    You have another loop following to print all those seen[i] values which are non-zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find repeated number in the sequence
    By abhi143 in forum C Programming
    Replies: 12
    Last Post: 04-10-2020, 08:34 AM
  2. Program to find repeated number in array
    By vajra11 in forum C Programming
    Replies: 9
    Last Post: 08-31-2018, 09:17 AM
  3. Recursion to find sum of digits of n digit number ?
    By justine in forum C Programming
    Replies: 7
    Last Post: 11-26-2012, 05:35 AM
  4. Find no of times a no is repeated in a array.
    By Anitrex in forum C Programming
    Replies: 4
    Last Post: 03-18-2012, 06:07 AM
  5. Check for repeated digits!
    By CrackerJack in forum C Programming
    Replies: 5
    Last Post: 11-08-2003, 11:37 PM

Tags for this Thread