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++
#include <bits/stdc++.h>
using namespace std;
bool isEven(string s)
{
int l = s.length();
bool dotSeen = false ;
for ( int i = l - 1; i >= 0; i--) {
if (s[i] == '0' && dotSeen == false )
continue ;
if (s[i] == '.' ) {
dotSeen = true ;
continue ;
}
if ((s[i] - '0' ) % 2 == 0)
return true ;
return false ;
}
}
int main()
{
string s = "100.70" ;
if (isEven(s))
cout << "Even" ;
else
cout << "Odd" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static boolean isEven(String s1)
{
int l = s1.length();
char [] s = s1.toCharArray();
boolean dotSeen = false ;
for ( int i = l - 1 ; i >= 0 ; i--) {
if (s[i] == '0' && dotSeen == false )
continue ;
if (s[i] == '.' ) {
dotSeen = true ;
continue ;
}
if ((s[i] - '0' ) % 2 == 0 )
return true ;
return false ;
}
return false ;
}
public static void main(String argc[])
{
String s = "100.70" ;
if (isEven(s))
System.out.println( "Even" );
else
System.out.println( "Odd" );
}
}
|
Python3
def isEven(s) :
l = len (s)
dotSeen = False
for i in range (l - 1 , - 1 , - 1 ) :
if (s[i] = = '0'
and dotSeen = = False ) :
continue
if (s[i] = = '.' ) :
dotSeen = True
continue
if (( int )(s[i]) % 2 = = 0 ) :
return True
return False
s = "100.70"
if (isEven(s)) :
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
public class GfG {
public static bool isEven( string s1)
{
int l = s1.Length;
bool dotSeen = false ;
for ( int i = l - 1; i >= 0; i--) {
if (s1[i] == '0' && dotSeen == false )
continue ;
if (s1[i] == '.' ) {
dotSeen = true ;
continue ;
}
if ((s1[i] - '0' ) % 2 == 0)
return true ;
return false ;
}
return false ;
}
public static void Main()
{
string s1 = "100.70" ;
if (isEven(s1))
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
}
|
PHP
<?php
function isEven( $s )
{
$l = strlen ( $s );
$dotSeen = false;
for ( $i = $l - 1; $i >= 0; $i --)
{
if ( $s [ $i ] == '0' && $dotSeen == false)
continue ;
if ( $s [ $i ] == '.' )
{
$dotSeen = true;
continue ;
}
if (( $s [ $i ] - '0' ) % 2 == 0)
return true;
return false;
}
}
$s = "100.70" ;
if (isEven( $s ))
echo "Even" ;
else
echo "Odd" ;
?>
|
Javascript
<script>
function isEven(s)
{
let l = s.length;
let dotSeen = false ;
for (let i = l - 1; i >= 0; i--)
{
if (s[i] == '0' && dotSeen == false )
continue ;
if (s[i] == '.' )
{
dotSeen = true ;
continue ;
}
if ((s[i] - '0' ) % 2 == 0)
return true ;
return false ;
}
}
let s = "100.70" ;
if (isEven(s))
document.write( "Even" );
else
document.write( "Odd" );
</script>
|
Time Complexity: O(|s|)
Auxiliary Space: O(1)