Conditional Statements in C

Sort by

recency

|

529 Discussions

|

  • + 2 comments

    include

    int main() { int x = 0;

    char arr[10][9] = {
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine"
    };
    
    
    
    scanf("%d", &x);
    
    
    
    if (x >= 0 && x <= 9) 
    {
        printf("%s\n", arr[x]);
    } else {
        printf("Greater than 9 \n");
    }
    
    return 0;
    

    }

  • + 0 comments

    Using Ternary Operator

    #include<stdio.h>
    
    int main() {
        int n;
    
        scanf("%d", &n);
    
        printf((n==1)?"one": (n==2)?"two":(n==3)?"three":(n==4)?"four":(n==5)?"five":(n==6)?"six":(n==7)?"seven":(n==8)?"eight":(n==9)?"nine":"Greater than 9");
    
        return 0;
    }
    
  • + 0 comments
    char *array[]={"one","two","three","four","five","six","seven","eight","nine"};
    
    if ((1<=n) && (n<=9))
    {
        printf("%s\n",array[n-1]);
    }
    else {
    printf("Greater than 9");
    }
    
  • + 0 comments
        char numberStrings[10][20] = {
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "Greater than 9"
        };
        
        int stringIndex = n > 9 ? 9 : n - 1;
    
        printf("%s", numberStrings[stringIndex]);
    
  • + 0 comments

    include

    int main() { int n; scanf("%d",&n); if(n==1) { prinf("one"); } else if(n==2) { printf("two"); } else if(n==3) { printf("three"); } else if(n==4) { printf("four"); } else if(n==5) { printf("five"); } else if(n==6) { printf("six"); } else if(n==7) { printf("seven"); } else if(n==8) { printf("eight"); } else if(n==9) { printf("nine"); } else if(n>9) { printf("greaterthan nine"); } }