+ 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; }

23rd Sep 2023, 5:12 AM
Theo Filus Iglesias Triadi
Theo Filus Iglesias Triadi - avatar
3 Réponses
+ 3
Your question does not look complete. Otherwise i think formating part of scanf should be overworked.
23rd Sep 2023, 6:41 AM
JaScript
JaScript - avatar
+ 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.
23rd Sep 2023, 10:24 AM
Brian
Brian - avatar
0
Could you please elaborate the issue in more details? What is the exact problem are you facing?
23rd Sep 2023, 6:32 AM
Avijit Pal
Avijit Pal - avatar