Comprobar si un número es capicúa (Máx 5 cifras) [ESP-ENG] | C++

avatar
(Edited)

Portadas para los post de programación.jpg

¡Hola! Por acá traigo un poco más programación, en esta ocasión vuelvo a desempolvar mi carpeta de Programación Orientada a Objetos (La materia que vi el año pasado), para repasar un ejercicio clásico. En este caso se trata de identificar si un número es capicua o no, pero... ¿Qué es capicua? Pues en este contexto es un numero que se escribe igual al derecho y al revés, ejemplo 22, 121, 1441, 33, 131.

Hello! Here I bring a little more programming, this time I dust off my Object Oriented Programming folder (the subject I saw last year), to review a classic exercise. In this case it is about identifying whether a number is capicua or not, but.... What is capicua? Well, in this context it is a number that is written the same upside down and backwards, e.g. 22, 121, 1441, 33, 131.


image.png


Programación c++.png

Dado que el código es corto, no hice múltiples capturas. En el código encontramos una variable de tipo long int (necesitaba un rango un poco superior al int). Luego un mensaje que le pide al usuario ingresar un numero entero de máximo 5 dígitos. Posteriormente entra a un if de un rango de 0 a 10 que dicta que todos los números de un dígito son capicúa (Esta seria una validación simple), luego al entrar al else if (sino, si), se asegura de que sea de dos dígitos con una comparación del rango, luego compara el módulo del numero ingresado con su división entre 10, ej: 10/10 = 1, 10%10=0, este número de ejemplo no sería capicúa. Conforme sube el número de cifras, se comparan más digitos, por lo que el parámetro del else if siguiente, es más largo.

Para el de cinco dígitos la comparación se pone un poco más compleja, Ej: 12321, el primero con el último, el segundo con el antepenúltimo y el central sin tocarlo. Dado que es un comparador hecho en un momento en el que no se nos permitía usar bucles, en ese caso no se repite la posibilidad de comparar otro número sin necesidad de volver a abrir la aplicación.

Since the code is short, I did not make multiple captures. In the code we find a variable of type long int (I needed a range a little higher than int). Then a message that asks the user to enter an integer of maximum 5 digits. Subsequently it enters an if of a range from 0 to 10 that dictates that all the numbers of a digit are capicua (This would be a simple validation), then when entering the else if (else, if), it makes sure that it is of two digits with a comparison of the range, then it compares the module of the number entered with its division by 10, ex: 10/10 = 1, 10%10=0, this number of example would not be capicua. As the number of digits increases, more digits are compared, so the next else if parameter is longer.

For the five digits the comparison becomes a little more complex, Ex: 12321, the first with the last one, the second with the antepenultimate one and the central one without touching it. Since it is a comparator made at a time when we were not allowed to use loops, in that case the possibility of comparing another number without having to reopen the application is not repeated.


image.png

Programación c++.png

image.png

image.png

image.png

image.png

Y pues eso es todo por hoy, espero les sea de gran ayuda, cuídense mucho y recuerden que el hecho de que mucha gente apoye una idea no significa necesariamente que sea correcta. Soli Deo Gloria!

Si te interesa otro tutorial de programación acá te dejo los links a los que he realizado hasta ahora:

And that's all for today, I hope it is of great help, take care and remember that the fact that many people support an idea does not necessarily mean that it is correct. Soli Deo Gloria!

If you are interested in another programming tutorial here are the links to the ones I have done until now:


Nombre de la publicación / Name of the postLINK
Calculador de datos para un triángulo rectángulo en C++1
Concecionario de autos, ejercicio en JAVA2
Calculador de edad básico C++3
Inversor de números de tres cifras4
¿Cuántos números de 3 cifras existen cuya suma sea 10? - ¡Respondiendo a otro usuario! C++6
Calculadora simple, cifra menos significativa y portada simple C++7
¡Vistazo a BeatUDO! [ESP-ENG] JAVA9
Comprobar si un número de dos cifras es primo [ESP-ENG] C++10
Conversor de pulgadas a 7 unidades distintas [ESP-ENG] C++11

Programación c++.png

Puedes seguirme por acá si lo deseas:
You can follow me here if you want:

Diseño sin título.gif

Cuenta secundaria
(Dibujos, edición y juegos) | Secondary account (Drawings, editing and games)



0
0
0.000
1 comments