Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problems in if else statements in C

#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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading