c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

c++

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

30th Oct 2021, 3:23 PM
Mando
Mando - avatar
21 Answers
+ 5
attempt: #include <iostream> using namespace std; bool isPalindrome() { int number, revnum=0; cin >> number; while (number != 0){ revnum *= 10; int lastd = number % 10; revnum += lastd; number/=10; } if (number=revnum ){ cout << number << " is a palindrome" << endl; } else cout << number << "is not a palindrome" << endl; } int main() { isPalindrome(); return 0; }
30th Oct 2021, 3:24 PM
Mando
Mando - avatar
+ 3
Martin Taylor that's beautiful🥺😊
2nd Nov 2021, 2:59 PM
Rishi
Rishi - avatar
+ 2
AJ oh wow thanks, btw congrats on top 5 sololearn.
30th Oct 2021, 4:21 PM
Mando
Mando - avatar
+ 2
Martin Taylor Long time no see =)
2nd Nov 2021, 1:55 AM
Rishi
Rishi - avatar
+ 1
AJ what do i apply that to
30th Oct 2021, 4:11 PM
Mando
Mando - avatar
+ 1
AJ still doesnt work😅
30th Oct 2021, 4:14 PM
Mando
Mando - avatar
+ 1
AJ you get a follow lol
30th Oct 2021, 4:21 PM
Mando
Mando - avatar
+ 1
#include <iostream> using namespace std; int main() { int n,r,sum=0,temp; cin>>n; temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) cout<<temp<<" is a Palindrome ."; else cout<<temp<<" is not Palindrome."; return 0; }
30th Oct 2021, 5:43 PM
Amit Patil
+ 1
From next time, try to describe your question in short(like the topic your question is based on) and not just the word "C++" everytime, as you're already tagging C++ you don't want to repeat yourself. All the best =)
1st Nov 2021, 6:52 AM
Rishi
Rishi - avatar
+ 1
Mando, Conversion from string to int and from int to string doesn't take less time read as string rather then int. That takes less time. See problem statement is has number not string. DHANANJAY
2nd Nov 2021, 4:08 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
AJ this is my attempt not a answer.
30th Oct 2021, 4:05 PM
Mando
Mando - avatar
0
Mando Single equal (=) is used for assignment not for comparison. Double equal (==) is used for comparison
30th Oct 2021, 4:06 PM
A͢J
A͢J - avatar
0
Mando Store number to temp then compare temp with revnum because number would be 0 (number /=10) so if condition would be false Also bool function should return 0 or 1. https://code.sololearn.com/cDqzuS1m0j8n/?ref=app
30th Oct 2021, 4:12 PM
A͢J
A͢J - avatar
0
Mando Check my code again.
30th Oct 2021, 4:15 PM
A͢J
A͢J - avatar
0
AJ did, same result.
30th Oct 2021, 4:16 PM
Mando
Mando - avatar
0
Mando Yes I have checked test cases again there is NOT instead of not
30th Oct 2021, 4:17 PM
A͢J
A͢J - avatar
0
AJ what do you mean
30th Oct 2021, 4:19 PM
Mando
Mando - avatar
0
Mando Check this line "is not a palindrome" Here not should be NOT
30th Oct 2021, 4:19 PM
A͢J
A͢J - avatar
0
Mando I was at 3rd 1 month ago since 1 years
30th Oct 2021, 4:44 PM
A͢J
A͢J - avatar
0
AJ nice
30th Oct 2021, 4:45 PM
Mando
Mando - avatar