Search code examples
convertersrestrictionnibble

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!


Solution

  • 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");