C - Write program to check yes no answer

Requirements

Ask for the answer to a yes-or-no question with a press of the Y or N key.

Either upper- or lowercase are accepted.

Ensure that the program responds properly when neither a Y nor N is pressed.

Hint

Use the || comparison twice. First for the Y key press and then for the N.

Use an if-else-if-else structure to handle all three variations.

Demo

#include <stdio.h>

int main()/*w  ww  . j a v a 2  s  . co m*/
{
    char yorn;

    printf("Do you want to continue (Y/N)? ");
    scanf("%c",&yorn);
    if( yorn == 'Y' || yorn == 'y' )
    {
        puts("Continuing...");
    }
    else if( yorn == 'N' || yorn == 'n' )
    {
        puts("Stopping now.");
    }
    else
    {
        puts("You didn't type Y or N!");
    }
    return(0);
}

Result

Related Exercise