+ 2
what is the problem of this code and why this code always run the "non-palindrome" even though i checked algorithm several times
#include <iostream> bool isPalindrome(int x){ int a,sum=0; while(x>0){ a=x%10; sum=sum*10+a; x=x/10; } } int main() { int x; int sum; std::cin>>x; if(isPalindrome(x)) {std::cout<<sum<<"is a palindrome";} else {std::cout<<sum<<"is not a palindrome";} }
15 ответов
+ 4
Possible problems are:
1) isPalindrome() returns no boolean,
2) it doesn't determine whether x is palindrome or not.
Cheer!
+ 3
Hi Martin Taylor
I agree that a string problem is easier to resolve, but I find this approach quite interesting.
+ 3
You haven't compared your input value and sum ......and then u haven't returned any Boolean value too...
This code will do...
#include <iostream>
bool isPalindrome(int p){
int a,sum=0,c=p;
while(c>0){
a=c%10;
sum=sum*10+a;
c=c/10;
}
if(p==sum)
return true;
else
return false;
}
int main()
{
int x;
std::cin>>x;
if(isPalindrome(x))
{std::cout<<x<<" is a palindrome";}
else
{std::cout<<x<<" is not a palindrome";}
}
+ 2
Yep, his code is very close. I hope he make it his way……
+ 1
Rik Wittkopp i have run the adjusted version which you recommend but the problem is why rhe out put is always non palindrom and doesnt give a right conditional , for example when you enter 252 it says it is non palindrome and its wrong.
+ 1
#include <iostream>
bool isPalindrome(int x){
int a,sum=0;
while(x>0){
a=x%10;
sum=sum*10+a;
x=x/10;
}
return sum;
if (sum==x)
return true;
else
return false;
}
int main()
{
int x;
//int sum;
std::cout<<isPalindrome(887);
std::cin>>x;
if(isPalindrome(x))
{std::cout<<x<<"is a palindrome";}
else
{std::cout<<x<<"is not a palindrome";}
}
But why now it returns always true and the conditionals doesnt work as it should?
Rik Wittkopp
Shizuku
Martin Taylor
+ 1
You have an excess return statement.
It returns always true and quits the program before if-else statement.
Note that any non-zero value will be considered true!
+ 1
But Shizuku is there a way to give the problem a function which qould have have the right boolean form?
You know how would i write this section?
+ 1
Just ONE malicious line, bro.
And mind you, inside main() should not have been altered.
Maybe original main() goes like:
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}
Good luck.
+ 1
Nariman Tajari
I know that you have received a number of solutions already, but I would like to present a working solution using the concepts of our discussion.
#include <iostream>
bool isPalindrome(int num){
int a,x,sum=0;
x = num;
while(x>0){
a=x%10;
sum=sum*10+a;
x=x/10;
}
return sum == num;
}
int main(){
int x;
std::cin>>x;
if(isPalindrome(x)){
std::cout<<x<<" is a palindrome";
}
else {
std::cout<<x<<" is not a palindrome";
}
}
0
Nariman Tajari
There are a number of problems with your code.
1. Your Boolean function does not work.
It did not have a return from the function so no result could be produced when the function was called.
Also, your variable sum =0, but later you want a value multiplied by sum, which will be 0, 6*0=0.
There is another problem within your logic but I am hoping you can resolve that yourself, (meaning I stopped looking for it)
2. in main() you have int sum.
You then attempt to get a boolean value from your function, followed by a statement referring to sum, which has not yet been assigned to a value.
I have modified your code a bit.
Fix the logic within your bool function & it should work
Delete the test boolean section when ready
0
Nariman Tajari
The adjusted code I mentioned.
#include <iostream>
bool isPalindrome(int x){
int a,sum=0;
while(x>0){
a=x%10;
sum=sum*10+a;
x=x/10;
}
return sum == x;
}
int main(){
// testing boolean function
std::cout << isPalindrome(262)<<std::endl;
int x;
//int sum;
std::cin>>x;
if(isPalindrome(x)){
std::cout<<x<<" is a palindrome";
}
else {
std::cout<<x<<" is not a palindrome";
}
}
0
Nariman Tajari
I fixed your bool function, which I will attach.
I have left all my test notes in place for you to review.
The problem was that x was being reduced to 0, while sum was being constructed
#include <iostream>
bool isPalindrome(int num){
int a,x,sum=0;
x = num;
std::cout<<x << std::endl;
while(x>0){
a=x%10;
sum=sum*10+a;
x=x/10;
//std::cout << a << std::endl;
std::cout << sum << std::endl;
}
std::cout << x << std::endl;
return sum == num;
}
int main(){
// testing boolean function
std::cout << isPalindrome(262)<<std::endl;
}
0
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <iostream>
int lengthValue(int );
bool isPalindrome(int );
int main( ){
std::cout<<std::boolalpha<<isPalindrome(555);
return 0;
}
int lengthValue( int value){
int i=0;
while ( value > 0){
value /= 10;
++i;
}
return i;
}
bool isPalindrome(int value){
int length=lengthValue(value);
std::vector<int> vec, vec2;
int i(0);
int divide;
for (int i=length-1; i >=0 ; --i){
divide = value / (int)std::pow(10,i);
value %= (int) std::pow(10,i);
vec.push_back(divide);
}
vec2 = vec;
std::reverse(vec2.begin(),vec2.end());
if ( vec == vec2)
return true;
return false;
}