+ 1
Help me to fix my code. The question asks to enter contraits 0 <= X, Y <= 10000. Please show me tutorial to enter contraits
#include <stdio.h> int main() { int x, y; scanf("%d+%d", &x, &y); printf("%d", x+y); return 0; }
3 Answers
+ 3
Your question does not look complete. Otherwise i think formating part of scanf should be overworked.
+ 2
Theo Filus Iglesias Triadi to validate the input values of x and y, you may add an if/else statement that checks the range after they are entered. If either one is NOT in range then print an error message. I give an example below to study:
if (!(0<=x))
puts("Error: X must be >= 0.");
else if (!(y<=10000))
puts("Error: Y must be <= 10,000.");
else
printf("%d", x+y);
Notice that I used the Logical Not operator, symbolized by the exclamation point (!). It negates the logic. So read it as "if it is not true that (0<=x)...", then display an error message.
Optionally, you could test for the opposite condition: "if (x<0)...", then display an error message. It is simpler to read.
0
Could you please elaborate the issue in more details? What is the exact problem are you facing?