Open In App

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

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

## Javascript

 ``

Output

`Odd`

Time Complexity: O(|s|)
Auxiliary Space: O(1)