programming puzzle (silly)

Pete Klier klier at ucbarpa.Berkeley.EDU
Fri Mar 10 12:04:48 AEST 1989


***********************
main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;);
printf(m?"Answer=%d\n":"error\n",m);}
***********************

with help from Jeff Conroy (jmconroy at ernie.Berkeley.EDU)
 
the expression "m=n>0^n>9" is equivalent to 
"m = (n > 0) && (n <= 9)" (check it out)
so "m" is 1 (initially) iff n is within range, 0 otherwise.
If m is 0 in the body of the loop, then the expression
(n&&m*=n--) is 0, since (m*=n--) is 0.  Otherwise, we
have an ordinary factorial loop, which begins with
m == 1.  
Pete Klier
klier at ernie.Berkeley.EDU



More information about the Comp.lang.c mailing list