+ 3

If I enter age more than 70 my else block isn't executing. Why?

#include <stdio.h> int main(int argc, char **argv) { int age; printf("Enter your age: "); scanf("%d", &age); printf("You entered %d as your age\n", age); if (age >= 18 && age <= 70){ printf("You can Drive!\n"); } else if (age > 15){ // printf("Your age is %d\n", age); if (18-age > 1){ printf("You can drive after %d years", 18-age); } if (18-age == 1) { printf("You can drive after %d year", 18-age); } } else{ printf("You cannot Drive\n"); } return 0; }

30th Apr 2023, 12:30 PM
Harshika Malhotra
Harshika Malhotra - avatar
2 ответов
+ 7
If you enter more than 70 then first if condition age<=18 && age <= 70 false but else if condition age > 15 is true but again inner if conditions both false so won't output next anything. Last else part works only if input is not above 15 You have if ( ... ) { .. } else if( ... ) { ... } else { .. } So it's all depends on your logic..
30th Apr 2023, 12:39 PM
Jayakrishna 🇮🇳
+ 8
Look at else if block. If you enter 70, the if clause is false. So it goes to else if clause. Is 70 > 15? True. So there is the problem. Your else if clause should look like this: else if (age > 15 && age < 18) Hope this helps.
30th Apr 2023, 1:15 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar