+ 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

16th Feb 2022, 9:29 AM
Billy Stanley
Billy Stanley - avatar
4 odpowiedzi
+ 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; }
16th Feb 2022, 11:42 AM
A͢J
A͢J - avatar
+ 2
It seems that your "while" is an infinite loop, since num is never updated.
16th Feb 2022, 9:56 AM
Geoffrey L
Geoffrey L - avatar
+ 1
Thank you
16th Feb 2022, 2:36 PM
Billy Stanley
Billy Stanley - avatar
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; }
16th Feb 2022, 9:30 AM
Billy Stanley
Billy Stanley - avatar