+ 1
How to properly use a bool function in c++
I am working on a code project that wants me to use a boolean function but the lessons till now used ether int functions or void functions so I can't figure out how to get it to return anything no matter what I try it just says no output
4 Answers
+ 4
Billy Stanley
Do num = num / 10
In while loop after backnum
And initialise backnum with 0
bool isPalindrome(int x) {
//complete the function
int num = x, backnum = 0, remainder = 0;
while (num != 0) {
remainder = num % 10;
backnum = backnum * 10 + remainder;
num = num / 10;
}
return backnum == x;
}
+ 2
It seems that your "while" is an infinite loop, since num is never updated.
+ 1
Thank you
0
My code
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int num = x, remainder, backnum;
while (num != 0) {
remainder = num % 10;
backnum = remainder + (backnum *10);
}
if (backnum == x) {
return true ;
}
else {
return false ;
}
}
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}