/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* repdigit.c (Chapter 8, page 166) */ /* Checks numbers for repeated digits */ #include #include /* We assume here that 0 represents FALSE, and 1 represents TRUE */ #define TRUE 1 #define FALSE 0 int main(void) { /* It is sufficient to initialize just one element of the array, since the remaining elements of the array are given the value 0 by default. It is illegal for an initializer to be empty. Also, it is illegal for an initializer to be longer than the array it initializes. If a complete initializer is present, it determines the length of the array, so the length of the array may be left unspecified explicitly. */ int digit_seen[10] = {FALSE}; int digit; long int n; /* For your information, long int has at least 32 bits in size, but on a computer with 64 bits CPU long int has 64 bits. In limits.h header file, a macro LONG_MAX defines the maximum value for a long int. */ printf("The value of LONG_MAX on this computer is %ld\n", LONG_MAX); printf("Enter a number: "); scanf("%ld", &n); while (n > 0) { digit = n % 10; if (digit_seen[digit]) break; digit_seen[digit] = TRUE; n = n / 10; } if (n > 0) printf("Repeated digit\n"); else printf("No repeated digit\n"); return 0; }