Hello, i have to create a program for-
If the age is less than 18-You are still a kid
18-30 adult
30-60 middle age
greater than 60-senior citizen
else for any other impossible age (-ve or 0 age) ' wow '

Here is my attempt,

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();

int n;
printf("Please enter your age ");
scanf("%d" , &n);

if (0<n<18) printf("You are still a kid");
else if (18<=n<30) {printf("You are an adult");
        else if (30<=n<60) {printf("You are a middle-aged person");
                 else if (60<=n) {printf("You are a senior citizen");
                         else {printf("Wow !!!");
}
}
}
}
getch();

}

My compiler says 'Misplaced else'
Please help me find my mistake.

Thank you.

if (0<n<18) printf("You are still a kid");
else if (18<=n<30) {printf("You are an adult");
        else if (30<=n<60) {printf("You are a middle-aged person");
                 else if (60<=n) {printf("You are a senior citizen");
                         else {printf("Wow !!!");

You have placed braces before printf. Remove them.

EDIT:

You code would look more appealing(read: not Horrible) if you put it like this & it would also make some logical sense

if (0<n<18) 
   printf("You are still a kid");
else if (18<=n<30) 
   printf("You are an adult");
else if (30<=n<60) 
   printf("You are a middle-aged person");
else if (60<=n) 
   printf("You are a senior citizen");
else 
   printf("Wow !!!");

If you decide to go for if-else ladder make sure to format them neatly or the code logic does NOT come quick.

Edited 6 Years Ago by nbaztec: n/a

Didn't see 2 more things:

else if (18<=n<30) 
.
.
else if (30<=n<60) 
.
.

This is illegal. You have to use the Logical Operator AND(&&)

else if (18<=n && n<30) 
.
.
else if (30<=n && n<60) 
.
.
This question has already been answered. Start a new discussion instead.