Home > Back-end >  Any idea why this isn't executing when age is less than 18 and greater than 55
Any idea why this isn't executing when age is less than 18 and greater than 55

Time:10-02

#include <stdio.h>
int main()
{
 int age,weight;
 printf("Enter your age: ");
 scanf("%d",&age);
 if (age>55 && age<=18)
     {printf("Not eligible");}
 if (age>18 && age<=55) 
    {printf("Enter your weight: ");
    scanf("%d",&weight);
    if (weight>45)  
        {printf("You satisfy all the eligibility criteria.");}
    else 
        {printf("You're not eligile for donating blood.");}}
 return 0;
}

The first "if" statement is not executing.The second "if" statement runs fine.

CodePudding user response:

The logic for the if conditionals is incorrect. Tell me: can you be teenager and middle-aged person at the same time?

CodePudding user response:

Flow and logic needed a bit of work, but you were almost there... just a couple minor adjustments.

#include <stdio.h>

int main(void)
{
    int age, weight;

    printf("Enter your age: ");
    scanf("%d", &age);
    
    if (age > 55 || age < 18)
    {
        printf("Not eligible.\n");
        return 1; /* EXIT_FAILURE */
    }
    else
    {
        printf("Enter your weight: ");
    
        scanf("%d",&weight);
    
        if (weight > 45)
            printf("You satisfy all the eligibility criteria.\n");
        else
            printf("You're not eligile for donating blood.\n");
    }
    
    return 0;
}

CodePudding user response:

#include <stdio.h>
int main()
{
 int age,weight;
 printf("Enter your age: ");
 scanf("%d",&age);
 if (age>55 || age<=18) // here you did logical error
     {
       printf("Not eligible");
     }
 if (age>18 && age<=55)
    {
      printf("Enter your weight: ");
      scanf("%d",&weight);
      if (weight>45)
        {
          printf("You satisfy all the eligibility criteria.");
        }
    else
        {
          printf("You're not eligible for donating blood.");}
        }
 return 0;
}

CodePudding user response:

You are misusing logic operators in the following line. (Hint: '&&')

if (age>55 && age<=18)

  •  Tags:  
  • c
  • Related