jeudi 18 mars 2021

How to identify if a number is odd or even by it's last digit in C++?

hello Im still new at programming so sorry if it's a dumb question but what's the error in my program? every number I input it will always say it's an odd number and the else statement doesn't work.

cout << "This program will determine whether a number is odd or even. \n";
cout << "Please enter a number ---> ";
cin >> number;

lastDigit = number % 10;

if ( number % 10 == 1,3,5,7,9)
{
    cout << "\nThe last digit is " << lastDigit;
    cout << endl << number << " is odd.";
}

else
{
    cout << "\nThe last digit is " << lastDigit;
    cout << endl << number << " is even.";
}

getch ();   
return 0;

}`

Aucun commentaire:

Enregistrer un commentaire