Hi, I am testing a below from book " C : a modern approach"..
I don't understand why this program is not working ... when i put 123, it digit_seen should still be false(intialized all w/ 0).. but it goes into true and breaks the loop.
Can someone see why it doesn't work?
Code:
/* Checking a number for repeated digits */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef int Bool;
main() {
Bool digit_seen[10] = {0};
int digit;
long int n;
printf("Enter a number: ");
scanf("%ld", &n);
while ( n > 0 ) {
digit = n % 10; /* 30 % 10 = 0 */
printf("digit is %d\n",digit);
/* printf("digi_Seen is %ld", digit_seen_[digit]); */
if ( digit_seen[digit] ) {
printf("you suck\n");
break;
}
digit_seen[digit] = TRUE;
printf("digi_Seen NOW is %s", digit_seen_[digit]);
printf("n is %ld\n",n);
n /= 10;
printf("n is %ld after division\n",n);
}
if ( n > 0 ) {
printf("Repeated digit\n\n");
} else {
printf("No repeated digit\n\n");
}
return 0;
}
// runnign this
user1@mint-laptop:~/program/c$ ./repeate_digit
Enter a number: 123
digit is 3
you suck
Repeated digit
user1@mint-laptop:~/program/c$