0

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!

Sali
  • 5
  • 1

1 Answers1

0

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");
twooBeers
  • 186
  • 2
  • 11
  • but can you still give me a possible example even with conditions maybe please ? – Sali Apr 27 '16 at 18:08
  • nice thank you so the && in the middle of the second line safes both inputs of n>0 and n<10 right? – Sali Apr 27 '16 at 18:44
  • i added the example above. by the way, you declared n as "int" -- its a signed var in most compilers. if you write "unsigned int" you can remove the first sub condition ( n>=0). – twooBeers Apr 27 '16 at 18:44
  • **&&** is a logical **AND**. so the condition on the left **and** right side must be true to get a true as a result – twooBeers Apr 27 '16 at 18:47