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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



Output :

odd


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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