0

If we enter any number > 12 it still shows valid please help me with my code

If and else statement https://code.sololearn.com/c8AhWHrtTsvN/?ref=app

24th Jun 2021, 9:47 AM
UJCoder
UJCoder - avatar
5 odpowiedzi
+ 1
if(mn>0 && mn<13)
24th Jun 2021, 9:58 AM
Flash
+ 1
because you gave: if(mn>1 || mn<12) it means if month is >1 or month is <12 it will print valid if any condition is fulfilled it will print valid that's why your code is giving valid even if it's greater than 13 because it's greater than 1. Use && operator instead of || and also give use <= and >=instead of< and > because if the month is 12 or 1 it should print valid. if(mn>=1 && mn<=12)
24th Jun 2021, 10:00 AM
The future is now thanks to science
The future is now thanks to science - avatar
0
I don't know any about c++ UDDHAV JOSHI but you should include && instead of || Because if statement checks the condition true or false. If it's is true --- it execute your code in If statement. Condition = false ------ it execute else statement. In your case, you use or conditional statements.. If either one is true.. If is executed. So use && there.. Any condition false, else statement executed.
24th Jun 2021, 10:05 AM
Kumar Pinninti
Kumar Pinninti - avatar
0
Well, the problem is, your understanding about || and && logical operators. You should know the meaning of if(mn >1 || mn<12)❌ It means that all mn that is greater than 1 is accepted, or all that mn less than 12 is accepted. All are valid and no number is wrong even if it's negative. Becasue either is right. The correct answer: if(mn>0 && mn<13)✔️ Or you can try this: If(mn>= && mn<=12)✔️ You should know how to read your code.
24th Jun 2021, 10:51 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
0
Thanks everyone for the solution!😊
24th Jun 2021, 11:05 AM
UJCoder
UJCoder - avatar