+ 2

Числа палиндромы

Напишите функцию, которая возвращает true, если данное число является палиндромом, и false, если не является. Завершите данную функцию, чтобы код в main работал и приводил к ожидаемому результату. Пример Входных Данных: 13431 Пример Выходных Данных: 13431 is a palindrome Вот я что написал: #include <iostream> using namespace std; bool isPalindrome(int x) { //завершите функцию for(x=0; x>10; x++) cout<<x<<endl; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; } The task says that I would complete the given function so that the code in main would work and lead to the expected result. As I understand it in main, I can't change anything.

17th Jan 2021, 7:24 PM
Алексей
15 Answers
+ 4
do { digit = num % 10; rev = (rev * 10) + digit; num = num / 10; } while (num != 0); Ex -- the number 123, 3 is in the zeroth place, 2 in the oneth place and 1 in the hundredth place. So, to add another number 4 after 123, we need to shift the current numbers to the left, so now 1 is in the thousandth place, 2 in the oneth place, 3 is in the onethplace and 4 in the zeroth place. This is done easily by multiplying 123 by 10 which gives 1230 and adding the number 4, which gives 1234. The same is done in the code above. When the do while loop finally ends, we have a reversed number in rev. This number is then compared to the original number n. If the numbers are equal, the original number is a palindrome, otherwise it's not. Edit : If u still not getting this topic u required to do more work on math : ) Don't worry you will get it surely . Just keep going : ))
17th Jan 2021, 8:16 PM
Mr. Rahul
Mr. Rahul - avatar
+ 4
Decided everything #include <iostream> using namespace std; bool isPalindrome(int n) { //завершите функцию int n0= n, m = 0; while(n!=0) { m *= 10; m += n%10; n = n/10; } return (m == n0); } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }
18th Jan 2021, 7:55 PM
Алексей
+ 4
Я решил,взял с инета.Код прост и понятен.А ваш это другой уровень навыков c++.Я новичок мне надо пока попроще.А ваш код мне даже не ясен, кроме последних 2 строк! Мне давали рабочий код, но толку если я его не понимаю.Таким образом не фига не усвою!.
18th Jan 2021, 8:46 PM
Алексей
+ 3
https://code.sololearn.com/cXNIN8gn3XI8/?ref=app Алексей See this : ) Simple solved using else if
17th Jan 2021, 7:32 PM
Mr. Rahul
Mr. Rahul - avatar
+ 3
Mr.Rahul. The code works. But I have a different construction of the code. I'll try to build on your code.
17th Jan 2021, 8:02 PM
Алексей
17th Jan 2021, 8:08 PM
Mr. Rahul
Mr. Rahul - avatar
+ 3
Я предлагаю сделать данную функцию через строки. Сначала мы конвертируем целочисленный тип в строку, обращаем ее, конвертируем в int. И сравниваем с данным числом. #include <iostream> #include <string> #include <algorithm> using namespace std; //Compiler version g++ 6.3.0 bool palindrome(int x){ string str = to_string(x); reverse(str.begin(), str.end()); int x_reverse=stoi(str); if(x==x_reverse) return true; else return false; }
18th Jan 2021, 8:41 PM
Павло Герасимчук
+ 2
Login does not work on the forum site
17th Jan 2021, 7:44 PM
Алексей
+ 2
That's not for you
17th Jan 2021, 7:52 PM
Алексей
+ 2
No works
17th Jan 2021, 8:14 PM
Алексей
+ 2
#include <iostream> using namespace std; bool isPalindrome(string x) { //завершите функцию for(int i=0; i<x.length()/2; ++i){ if(x[i] != x[x.length()-i-1]) return false; } return true; } int main() { string n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout <<n<<" is NOT a palindrome"; } return 0; }
14th Sep 2021, 5:30 PM
Андрей Галеев
Андрей Галеев - avatar
+ 1
A palindromic number is a number (such as 626) that remains the same when its digits are reversed. Write a function that returns true if a given number is a palindrome, and false, if it is not. Complete the given function, so that the code in main works and results in the expected output. Sample Input: 13431 Sample Output: 13431 is a palindrome
17th Jan 2021, 7:26 PM
Алексей
+ 1
The task says that I would complete the given function so that the code in main would work and lead to the expected result. As I understand it in main, I can't change anything. I need to write a function for the code in main to work with the function. A palindromic number is a number (such as 626) that remains the same when its digits are reversed. Write a function that returns true if a given number is a palindrome, and false, if it is not. Complete the given function, so that the code in main works and results in the expected output. Sample Input: 13431 Sample Output: 13431 is a palindrome
17th Jan 2021, 9:52 PM
Алексей