简体   繁体   中英

Nibble Restriction to 0-9

Hello fellow Programmers,

int main() int n;

scanf("%d", &n ,);
printf("nibble =  %d%d%d%d", (n/8)%2, (n/4)%2, (n/2)%2 , n%2 );

return 0;}

I´ve build this code so far, the code converts a decimal value into a nibble.

It works but i have questions regarding on how to restrict the input to 0-9, without using if statemens. Is there any possiblty to do that and if how ? At the moment the code uses 0 - 15 decimals.And can you please give me an example or explain to me so i can understand it .

Thank you!

that is not possible without using conditions

but tell us what your program should do if a decimal >9 is putted in?

scanf("%d", &n ,);
if ((n>=0) && (n<10))
  printf("nibble =  %d%d%d%d", (n/8)%2, (n/4)%2, (n/2)%2 , n%2 );
else
  printf("err: input out of range");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM