简体   繁体   中英

C Program Not Executing Else Statement

My program executes the if statement but not the else clause. Here are the conditions for the code: For each integer, n, in the interval [a,b] (given as input): 1 If 1<=n<=9, then print the English representation of it in lowercase. That is "one" for 1, "two" for 2, etc. 2 If n>9 and it is an even number, then print "even" 3. If n>9 and it is an odd number, then print "odd"

The first condition works fine, but when the code reaches the else clause, a symbol is displayed.

Code:

    #include <stdio.h>

    #include <stdio.h>

    #include <string.h>

    #include <math.h>

    #include <stdlib.h>

    int main()

    {

    int a, b, n;

    char English[10][10]={"","one","two","three","four","five","six","seven","eight","nine"};

    scanf("%d\n%d", &a, &b);

    for(n=a;n<=b;n++)
    {
     if (1<=n<=9)
         printf("%s\n", English+n);

     else
        {
        if(n%2==0)
         printf("even\n");

        else
         printf("odd\n");
        }
}

return 0;
    }

Input:

    8
    11

Output:

    eight
    nine
    ♂

Expected Output:

    eight
    nine
    even
    odd

It does not work in C. It always evaluates to the truth. Why? the result of 1 <= n is 0 or 1 . Both are always smaller than 9

if (1<=n<=9)

It should be

     if (n >= 1 && n <= 9)
         printf("%s\n", English[n]);

https://godbolt.org/z/hQu4w9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM