- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether given floating point number is even or odd in Python
Suppose we have a floating point number; we have to check whether the number is odd or even. In general, for integer it is easy by dividing the last digit by 2. But for floating point number it is not straight forward like that. We cannot divide last digit by 2 to check if it is odd or even.
So, if the input is like n = 200.290, then the output will be Odd though the last digit is divisible by 2.
To solve this, we will follow these steps −
- s := convert number as string
- flag := False
- for i in range size of s - 1 to 0, decrease by 1, do
- if s[i] is '0' and flag is False, then
- go for next iteration
- if s[i] is same as '.', then
- flag := True
- go for next iteration
- if s[i] is even, then
- return "Even"
- return "Odd"
- if s[i] is '0' and flag is False, then
Let us see the following implementation to get better understanding −
Example Code
def solve(n) : s = str(n) flag = False for i in range(len(s) - 1, -1, -1) : if s[i] == '0' and flag == False : continue if s[i] == '.': flag = True continue if int(s[i]) % 2 == 0 : return "Even" return "Odd" n = 200.290 print(solve(n))
Input
200.290
Output
Odd
- Related Articles
- 8085 program to check whether the given number is even or odd
- C++ Program to Check Whether Number is Even or Odd
- Check whether the length of given linked list is Even or Odd in Python
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Java Program to Check Whether a Number is Even or Odd
- Java program to find whether given number is even or odd
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Check whether product of 'n' numbers is even or odd in Python
- How to Check if a Number is Odd or Even using Python?
- Check whether the given number is Euclid Number or not in Python
- Check if count of divisors is even or odd in Python
- Program to check whether given number is Narcissistic number or not in Python
- Check whether the given number is Wagstaff prime or not in Python
- C++ Queries on Probability of Even or Odd Number in Given Ranges
- Python Program for Check if the count of divisors is even or odd

Advertisements