repeated digit

this code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <iostream>
#define TRUE 1
#define FALSE 0

using namespace std;

typedef int Bool;


int main()
{


Bool digit_seen[10]= {0};
int digit;
long int n;
int x;
 
    printf("Enter a number: ");
    scanf("%ld", &n);
 
    while(n > 0){
        digit = n % 10;
         
        if(digit_seen[digit]) 
            break;
        digit_seen[digit]= TRUE;  
       
        
        
        if(digit_seen[digit]=TRUE)
        {
          printf("Repeated digit:%d\n" , digit);
        }   
 
}

return 0;

}


gives repeated digits in an integer but only in one condition :

only if the repeated digit is the result of n%10 where n is the integer the user writes. If the repeated digit is not the result of n%10 , then the compiler gives a wrong result.

so the question is : how to make this code gives the repeated digit in an integer (regardless the fact that the repeated digit is the result of n%10 or not and especially with making the minimum of changes on the code)????????? ?????

This will work (minimal changes):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <cstdio> // iostream is not guaranteed to have printf and scanf
#define TRUE 1
#define FALSE 0

using namespace std;

typedef int Bool;

int main()
{
    Bool digit_seen[10]= {0};
    int digit;
    long int n;
    int x;

    printf("Enter a number: ");
    scanf("%ld", &n);

    while(n > 0){
        digit = n % 10;

        if(digit_seen[digit]) 
        {
            printf("Repeated digit:%d\n" , digit); // Moved this line
            break;
        }

        digit_seen[digit]= TRUE;
        n /= 10; // Had to add this line to avoid single-numbers
    }

    return 0;
}
Enter a number: 23481564
Repeated digit:4
Last edited on
this is concerning my understanding to the code :

for example:

n=1223 , n>0

digit = n%1223 = 3



digit_seen[3]=TRUE

the n/=10 is not to avoid single numbers but to break the number digit by digit

like 1223 becomes

3
2
2
1

so digit_seen[3]=TRUE , it means , according to my understanding, that
3=TRUE
2=FALSE
2=FALSE
1=FALSE


or the if condition will print only the TRUE numbers

3=TRUE
2 is it 3 no so 2 is FALSE
2 is it 3 no so 2 is FALSE
1 is it 3 no so 1 is false


so normally the code will print only the number 3 ; if we delete too the :

(n /= 10; // Had to add this line to avoid single-numbers)

the code only will print the number 3.

so how do we explain the fact that the code print in case of 1223 :

repeated digit : 2

meanwhile it should print :

repeated digit :3

???????????????

and what is wrong in all what I wrote concerning my understanding??????

I need a correction to my understanding





Why would it print repeated digit: 3? In the case of 1223, 3 is not the repeated digit, only 2 is.

Let me describe each line:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
scanf("%ld", &n); // read the number

while(n > 0){ // While we are not done analyzing the number
    digit = n % 10; // Get the least significant digit

    if(digit_seen[digit]) // Have we seen this digit before?  If this is the first time, then it's a no
    {
        printf("Repeated digit:%d\n" , digit); // Print the digit only because it's not the first time we've seen it
        break; // exit the loop
    }

    digit_seen[digit]= TRUE; // Well, we've seen this digit now, let's memorize that 
                              // so if we see the digit again, we can exit

    n /= 10; // Remove that least significant digit, shift everything to the right.
} // Try with the next digit 
Topic archived. No new replies allowed.