What happens in the code?

0 like 0 dislike
3 views
Good day! Learn the basics of programming in C++ example, and I can not understand with one example. There is a code that determines if a number is a palindrome (for Example 12321 is a palindrome, mirrored). Explain to me please, step by step, for example, a five-digit number (for example, 34543) what happens in the code at the stage where the system assigns the variables a, b, c, d, e:
#include using namespace std; int main() { int integer, a, b, c, d, e; cout << "Vvedite celoye 5-znachnoye chislo: "; cin >> integer; if (integer >= 10000 && integer <= 99999) { a = integer / 10000; integer %= 10000; b = integer / 1000; integer %= 1000; c = integer / 100; integer %= 100; d = integer / 10; integer %= 10; e = integer / 1; if (a == e && b == d) cout << "Eto Palindrom!" << endl; else cout << "Ne Palindrom!" << endl; } else cout << "Vi vveli nevernoye chislo\"; return 0; }


Thanks in advance.
by | 3 views

4 Answers

0 like 0 dislike
The first time I see such a "terrible" implementation checks the mirror, but nevertheless. It's simple: this example compares the digits of numbers: a tens of thousands b thousands, with hundreds, d - tens e - units. Accordingly, if the discharge is a discharge equal to e , and also category b is equal to the discharge d, the number of the mirror, otherwise - no. How do you get the digits, respectively, dividing the number by 10000, 1000, 100 and 10 each time the next discharge is calculated from a modulo (%=)of the previous result, which every time put in integer

integer = 34543 a = 34543 / 10000 // = 3 integer = 34543 %= 10000 // = 4543 b = 4543 / 1000 // = 4 integer = 4543 %= 1000 // = 543 c = 543 / 100 // = 5 integer = 543 %= 100 // = 43 d = 43 / 10 // = 4 integer = 43 %= 10 // =3 e = 3 / 1 // = 3
by
0 like 0 dislike
Very limited and genocidist implementation check number for palindrome. It is better to work with input as a string, thus will be more versatile function.

Example of js and C++ will be a similar algorithm.
let isPalindrome = function(string) { for (let i = 0; i < string.length / 2; i++) { if (string[i] !== string[string.length - i - 1]) { return false; } } return true; };
by
0 like 0 dislike
Receive tens of thousands, thousands, hundreds, tens, units. Something like that.
by
0 like 0 dislike
in a,b,c,d,e stupidly recorded the digits of the number.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
33,866 users