How to check if the last digit of a float number in odd or even in C++

1 Answer

0 votes
#include <iostream>

using namespace std;

bool last_digit_even(string s) { 
    if ((s[s.length() - 1] - '0') % 2 == 0) 
          return true; 

    return false; 
} 

int main() 
{
    string s = "3.14"; 
    if (last_digit_even(s)) 
        cout << "Even" << endl; 
    else
        cout << "Odd" << endl; 
        
    
    s = " 3.14159"; 
    if (last_digit_even(s)) 
        cout << "Even" << endl;  
    else
        cout << "Odd" << endl;  
}



/*
run:

Even
Odd

*/

 



answered Aug 24, 2019 by avibootz

Related questions

1 answer 215 views
1 answer 199 views
1 answer 212 views
1 answer 216 views
1 answer 206 views
...