Dado un número de punto flotante N , la tarea es comprobar si es palíndromo o no.
Entrada: N = 123.321
Salida: Sí
Entrada: N = 122.1
Salida: No
Acercarse:
- Primero, convierta el número de punto flotante dado en una array de caracteres.
- Inicialice el índice bajo al primer índice y el alto al último índice.
- Mientras bajo < alto:
- Si el carácter bajo no es igual al carácter alto, salga e imprima «No».
- Si el carácter en el nivel bajo es igual al carácter en el nivel alto, continúe después de incrementar el nivel bajo y disminuir el nivel alto…
- Si el ciclo anterior se completa con éxito, imprima «Sí».
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if num is palindrome bool isPalindrome(float num) { // Convert the given floating point number // into a string stringstream ss; ss << num; string s; ss >> s; // Pointers pointing to the first and // the last character of the string int low = 0; int high = s.size() - 1; while (low < high) { // Not a palindrome if (s[low] != s[high]) return false; // Update the pointers low++; high--; } return true; } // Driver code int main() { float n = 123.321f; if (isPalindrome(n)) cout << "Yes"; else cout << "No"; return 0; } // This code is contributed by Rajput-Ji
Java
// Java implementation of the approach public class GFG { // Function that returns true if num is palindrome public static boolean isPalindrome(float num) { // Convert the given floating point number // into a string String s = String.valueOf(num); // Pointers pointing to the first and // the last character of the string int low = 0; int high = s.length() - 1; while (low < high) { // Not a palindrome if (s.charAt(low) != s.charAt(high)) return false; // Update the pointers low++; high--; } return true; } // Driver code public static void main(String args[]) { float n = 123.321f; if (isPalindrome(n)) System.out.print("Yes"); else System.out.print("No"); } }
Python3
# Python3 implementation of the approach # Function that returns true if num is palindrome def isPalindrome(num) : # Convert the given floating point number # into a string s = str(num) # Pointers pointing to the first and # the last character of the string low = 0 high = len(s) - 1 while (low < high): # Not a palindrome if (s[low] != s[high]): return False # Update the pointers low += 1 high -= 1 return True # Driver code n = 123.321 if (isPalindrome(n)): print("Yes") else: print("No") # This code is contributed by ihritik
C#
// C# implementation of the approach using System; class GFG { // Function that returns true // if num is palindrome public static bool isPalindrome(float num) { // Convert the given floating point number // into a string string s = num.ToString(); // Pointers pointing to the first and // the last character of the string int low = 0; int high = s.Length - 1; while (low < high) { // Not a palindrome if (s[low] != s[high]) return false; // Update the pointers low++; high--; } return true; } // Driver code public static void Main() { float n = 123.321f; if (isPalindrome(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Function that returns true if num is palindrome function isPalindrome(num) { // Convert the given floating point number // into a string var s = num.toString(); // Pointers pointing to the first and // the last character of the string var low = 0; var high = s.length - 1; while (low < high) { // Not a palindrome if (s[low] != s[high]) return false; // Update the pointers low++; high--; } return true; } // Driver code var n = 123.321; if (isPalindrome(n)) document.write( "Yes"); else document.write( "No"); </script>
Producción:
Yes
Complejidad temporal : O(N).
Espacio Auxiliar : O(1).
Publicación traducida automáticamente
Artículo escrito por sunilkannur98 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA