Search code examples
cif-statementscanflogical-operators

Program prints out declined for all variables entered, but I am trying to make it only print that out when a specific letter is entered e.g.'u'?


I'm trying to create a program that will print Accepted when a letter of the alphabet it entered, excluding u. When u is entered it prints declined.

#include <stdio.h>

int main(void) {
    char val;

    printf("Enter your letter : \n");
    val = scanf("%c", &val);
    
    if (val == 'u' || (val >= '0' || val <= '9'))
        printf("DECLINED\n");
    else
        print("ACCEPTED\n");

    return (0);
}

Solution

  • If you only want to accept letters i think you should convert char to int (ASCII values) and check like this

          #include <stdio.h>
    
          int main () {
          char val;
          int c;
          printf("Enter your letter :\n");
    
          scanf("%c", &val);
          c = (int) (val);
    
          if ( ( val != 'u')   &&  (c >= 97 && c <= 122) ||  (c >= 65 && c <= 90)){
                 printf("ACCEPTED\n");
          } else {
                 printf("DECLINED\n");
          }
    
    
          return (0);
           }