Check whether the given floating point number is a palindrome
Last Updated :
13 Aug, 2021
Given a floating-point number N, the task is to check whether it is palindrome or not.
Input: N = 123.321
Output: Yes
Input: N = 122.1
Output: No
Approach:
- First, convert the given floating-point number into a character array.
- Initialize the low to first index and high to the last index.
- While low < high:
- If the character at low is not equal to the character at high then exit and print “No”.
- If the character at low is equal to the character at high then continue after incrementing low and decrementing high…
- If the above loop completes successfully then print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome( float num)
{
stringstream ss;
ss << num;
string s;
ss >> s;
int low = 0;
int high = s.size() - 1;
while (low < high)
{
if (s[low] != s[high])
return false ;
low++;
high--;
}
return true ;
}
int main()
{
float n = 123.321f;
if (isPalindrome(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class GFG {
public static boolean isPalindrome( float num)
{
String s = String.valueOf(num);
int low = 0 ;
int high = s.length() - 1 ;
while (low < high) {
if (s.charAt(low) != s.charAt(high))
return false ;
low++;
high--;
}
return true ;
}
public static void main(String args[])
{
float n = 123 .321f;
if (isPalindrome(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isPalindrome(num) :
s = str (num)
low = 0
high = len (s) - 1
while (low < high):
if (s[low] ! = s[high]):
return False
low + = 1
high - = 1
return True
n = 123.321
if (isPalindrome(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static bool isPalindrome( float num)
{
string s = num.ToString();
int low = 0;
int high = s.Length - 1;
while (low < high)
{
if (s[low] != s[high])
return false ;
low++;
high--;
}
return true ;
}
public static void Main()
{
float n = 123.321f;
if (isPalindrome(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPalindrome(num)
{
var s = num.toString();
var low = 0;
var high = s.length - 1;
while (low < high)
{
if (s[low] != s[high])
return false ;
low++;
high--;
}
return true ;
}
var n = 123.321;
if (isPalindrome(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...