Числа палиндромы | SoloLearn: Learn to code for FREE!

+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.

c++

1/17/2021 7:24:27 PM

Алексей

14 Answers

New Answer

+4

https://code.sololearn.com/cXNIN8gn3XI8/?ref=app Алексей See this : ) Simple solved using else if

+3

Алексей See this explanation how to reverse the input : ) https://www.programiz.com/cpp-programming/examples/palindrome-number

+3

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 : ))

+2

Mr.Rahul. The code works. But I have a different construction of the code. I'll try to build on your code.

+2

Я решил,взял с инета.Код прост и понятен.А ваш это другой уровень навыков c++.Я новичок мне надо пока попроще.А ваш код мне даже не ясен, кроме последних 2 строк! Мне давали рабочий код, но толку если я его не понимаю.Таким образом не фига не усвою!.

+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

+1

Login does not work on the forum site

+1

That's not for you

+1

No works

+1

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; }

+1

Я предлагаю сделать данную функцию через строки. Сначала мы конвертируем целочисленный тип в строку, обращаем ее, конвертируем в 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; }

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. 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