Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested if statement in C

Tags:

c

if-statement

My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.

For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.

How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.

#include <stdio.h>

int main()
{

    int length;
    char gender;

    printf("enter gender: ");
    scanf("%c", &gender);

    printf("enter length: ");
    scanf("%d", &length);

    if (gender == 'M' || 'm') {

        if (length <= 140 && length <= 160 ) {

            printf ("short");
        }

        if (length <= 161 && length <= 180 ) {

            printf ("medium");

        }

        if (length <= 181 && length <= 199 ) {

            printf ("tall");
        }

        if (gender == 'W' || gender == 'w') {

            if (length <= 120 || length <=140) {

                printf("short");

                if (length <=141 || length <=165) {

                    printf ("medium");

                    if (length <=166 || length <=180) {

                        printf ("tall");}

                        else {

                            printf("error");
                        }
                    }
                }
            }
        }



    return 0;
}
like image 464
kadir Avatar asked Nov 03 '25 07:11

kadir


1 Answers

For below lines, it has to be length >= 161. Pls check similar lines for this.

   if (length <= 161 && length <= 180 ) {

        printf ("medium");

    }
like image 128
anand Avatar answered Nov 05 '25 23:11

anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!