Check whether given floating point number is even or odd
Given a floating point number, check whether it is even or odd.
We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can’t check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number but its last digit is divisible by 2.
Examples :
Input : 100.7 Output : odd Input : 98.8 Output : even Input : 100.70 Output : odd Trailing 0s after dot do not matter.
Approach :
- We start traversing a number from its LSB until we get a non-zero digit or ‘.’
- If the number is divisible by 2 it is even else odd
- If it is ‘.’ than it means decimal part of number is traversed and now we can check number is even or odd by dividing number by 2 whether it is 0 or non zero digit.
Below is the implementation of above approach :
C++
// CPP program to check whether given floating // point number is even or odd #include <bits/stdc++.h> using namespace std; // Function to check even or odd. bool isEven(string s) { int l = s.length(); // Loop to traverse number from LSB bool dotSeen = false; for (int i = l - 1; i >= 0; i--) { // We ignore trailing 0s after dot if (s[i] == '0' && dotSeen == false) continue; // If it is '.' we will check next digit and it // means decimal part is traversed. if (s[i] == '.') { dotSeen = true; continue; } // If digit is divisible by 2 // means even number. if ((s[i] - '0') % 2 == 0) return true; return false; } } // Driver Function int main() { string s = "100.70"; if (isEven(s)) cout << "Even"; else cout << "Odd"; return 0; } |
Java
// Java program to check whether given floating // point number is even or odd import java.util.*; import java.lang.*; public class GfG { // Function to check even or odd. public static boolean isEven(String s1) { int l = s1.length(); char[] s = s1.toCharArray(); // Loop to traverse number from LSB boolean dotSeen = false; for (int i = l - 1; i >= 0; i--) { // We ignore trailing 0s after dot if (s[i] == '0' && dotSeen == false) continue; // If it is '.' we will check next digit and it // means decimal part is traversed. if (s[i] == '.') { dotSeen = true; continue; } // If digit is divisible by 2 // means even number. if ((s[i] - '0') % 2 == 0) return true; return false; } return false; } // Driver function public static void main(String argc[]) { String s = "100.70"; if (isEven(s)) System.out.println("Even"); else System.out.println("Odd"); } } /* This code is contributed by Sagar Shukla */ |
Python3
# Python 3 program to check # whether given floating # point number is even or odd # Function to check # even or odd. def isEven(s) : l = len(s) # Loop to traverse # number from LSB dotSeen = False for i in range(l - 1, -1, -1 ) : # We ignore trailing # 0s after dot if (s[i] == '0' and dotSeen == False) : continue # If it is '.' we will # check next digit and it # means decimal part is # traversed. if (s[i] == '.') : dotSeen = True continue # If digit is # divisible by 2 # means even number. if ((int)(s[i]) % 2 == 0) : return True return False # Driver Function s = "100.70"if (isEven(s)) : print("Even") else : print("Odd") # This code is contributed # by Nikita Tiwari. |
C#
// C# program to check whether given floating // point number is even or odd using System; public class GfG { // Function to check even or odd. public static bool isEven(string s1) { int l = s1.Length; // char[] s = s1.toCharArray(); // Loop to traverse number from LSB bool dotSeen = false; for (int i = l - 1; i >= 0; i--) { // We ignore trailing 0s after dot if (s1[i] == '0' && dotSeen == false) continue; // If it is '.' we will check next // digit and it means decimal part // is traversed. if (s1[i] == '.') { dotSeen = true; continue; } // If digit is divisible by 2 // means even number. if ((s1[i] - '0') % 2 == 0) return true; return false; } return false; } // Driver function public static void Main() { string s1 = "100.70"; if (isEven(s1)) Console.WriteLine("Even"); else Console.WriteLine("Odd"); } } /* This code is contributed by Vt_m */ |
PHP
<?php // PHP program to check // whether given floating // point number is even or odd // Function to check // even or odd. function isEven($s) { $l = strlen($s); // Loop to traverse // number from LSB $dotSeen = false; for ($i = $l - 1; $i >= 0; $i--) { // We ignore trailing // 0s after dot if ($s[$i] == '0' && $dotSeen == false) continue; // If it is '.' we will // check next digit and it // means decimal part is // traversed. if ($s[$i] == '.') { $dotSeen = true; continue; } // If digit is divisible by 2 // means even number. if (($s[$i] - '0') % 2 == 0) return true; return false; } } // Driver Code $s = "100.70"; if (isEven($s)) echo "Even"; else echo "Odd"; // This code is contributed by aj_36 ?> |
Output :
odd
Recommended Posts:
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Smallest number to multiply to convert floating point to natural
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- C Program to Multiply two Floating Point Numbers
- Program to find GCD of floating point numbers
- Check whether the point (x, y) lies on a given line
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Check whether a given point lies on or inside the rectangle | Set 3
- Check whether a point lies inside a sphere or not
- How to check if a given point lies inside or outside a polygon?
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies inside a triangle or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t



