mercredi 1 juillet 2020

I'm new to programming and I've written this basic program that checks if the user's input is an even or odd number

Everything works well except one thing and that's the last else where I wanted the program to output invalid input if the user inputted anything other than an integer but for some reason it prints out the condition if the input was 0 and outputs 0 is a nonnegative nor positive so I'd appreciate if someone tell me why it's doing that

/*
 Exercise - Write a program to check whether a given number is ODD or Even
 */
    
    cout << "Enter an integer: ";
    cin >> input;
    
    if(input % 2 == 0 && input != 0) {
        cout << input  << " is an even "
        "number" << endl;
    }else if(input % 2 != 0) {
        cout << input  << " is an odd "
        "number" << endl;
    }else if(input == 0) {
        cout << input << " is a nonnegative "
        <<"nor positive" << endl;
    }else
        cout << "Invalid input!" << endl;
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire