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
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;
}
+ 3
Martin Taylor that's beautiful🥺😊
+ 2
AJ oh wow thanks, btw congrats on top 5 sololearn.
+ 2
Martin Taylor Long time no see =)
+ 1
AJ what do i apply that to
+ 1
AJ still doesnt work😅
+ 1
AJ you get a follow lol
+ 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;
}
+ 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 =)
+ 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
0
AJ this is my attempt not a answer.
0
Mando
Single equal (=) is used for assignment not for comparison.
Double equal (==) is used for comparison
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
0
Mando
Check my code again.
0
AJ did, same result.
0
Mando
Yes I have checked test cases again there is NOT instead of not
0
AJ what do you mean
0
Mando
Check this line "is not a palindrome"
Here not should be NOT
0
Mando
I was at 3rd 1 month ago since 1 years
0
AJ nice