2
#include<stdio.h>
#include<stdlib.h>

int main(){

int month, day;
printf("Enter the input : ");
scanf("%d %d",&month,&day);

if (day == 1 &&  month==1 || month == 2 || month == 3 || month ==4){
    printf("Green\n");
}
else if(day == 2 && month == 5 || month == 6 || month ==7 || month ==8 ){
    printf("Red");
}

return 0;
}

In the above code whenever I choose d = 1 and month = 1-4 , it is supposed to print green which it does correctly. The problem is when I choose day = 2 & month = 8 or 7 or 6 it is supposed to print red but it is printing green. Am I missing something here?

4
  • 2
    Did you read this: en.cppreference.com/w/c/language/operator_precedence ? Commented Mar 22, 2022 at 15:40
  • Enclose the months in a paranthesis for clarity. (day == 1 && (month==1 || month == 2 || month == 3 || month ==4)) Commented Mar 22, 2022 at 15:41
  • Yeah. I missed the parenthesis. Thanks for the help. Commented Mar 22, 2022 at 15:50
  • 1
    @SparKot not only for clarity but rather for correctness. Commented Mar 22, 2022 at 15:53

2 Answers 2

3

you need to check the day and month diffently. Try this instead:

if (day == 1 &&  (month==1 || month == 2 || month == 3 || month ==4)){
    printf("Green\n");
}
else if(day == 2 && (month == 5 || month == 6 || month ==7 || month ==8)){
    printf("Red");
}
Sign up to request clarification or add additional context in comments.

Comments

0

It just works fine as you expected.
Maybe, you forgot that your first input is month and day is second. Try again now.

#include <stdio.h> 
#include<stdlib.h> 
int main(){ 
int month, day; 
printf("Enter the input : "); 
scanf("%d %d",&month,&day); 
if ((day == 1 && month==1) || month == 2 || month == 3 || month ==4)
{ printf("Green\n"); } 
else if(day == 2 && month == 5 || month == 6 || month ==7 || month ==8 ){ 
printf("Red"); } 
return 0; 
}

8 Comments

This is plain wrong and your code indentation is terrible. You name suggests that you know that indentation is most important in Python. In C it's also important, but not for defining the program structure but for readability
I just copied his code and paste it here. Since && has higher precedence than || operator parenthesis doesn't matter. It's just gives a warning not an error. And I believe input is provided reverse is the actual reason for this question.
Sorry, this answer is wrong, and your code formatting is terrible. The OP has accepted the other answer which is correct. Have a look at it. You probably misunderstood something.
That's what I told you earlier(copy paste). You can take some time and read his question again. If your inputs are 7 and 2 it returns Green while Red is returned for 2 and 7. #Order of the inputs is the actual reason op to confuse*
While input order does matter, this code still has issues; it will print "Green" if you enter 2 for the day and 4 for the month. The problem the OP is facing (and has acknowledged) is that && has higher precedence than ||, so the "Green" branch will be taken if month is 2, 3, or 4, regardless of the value of day. That's clearly not what the OP intends. Yeah, sure, this code produces the expected output for a specific set of inputs, but it doesn't produce expected outputs for all sets of inputs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.