0

Palindrome number checker in c++ with stack

I want to check if a number is palindrome or not using stack in c++. I have no idea what's wrong with this code. If you find out how to fix the error, kindly tell me. Here is my code: #include <iostream> #define size 4 using namespace std; int stack[size], TOS=0, number, n, num1, num2, num3, num_mod, num1_mod, num2_mod; int digitSeparator(int num){ number = num; num1 = num / 1000; num_mod = num % 1000; return num1; num2 = num_mod / 100; num1_mod = num_mod % 100; return num2; num3 = num1_mod / 10; return num3; num2_mod = num1_mod % 10; return num2_mod; } void push(int value){ if (TOS >= size){ cout << "Stack Full"; } else{ stack[TOS]=value; TOS++; } } int pop(){ TOS--; return stack[TOS]; } int main() { int reversed_number, digit_1, digit_2, digit_3, digit_4; cout<<"Enter a 4 digit number: " << endl; cin >> n; digitSeparator(n); push (num1); push (num2); push (num3); push (num2_mod); digit_1 = pop(); cout << digit_1; digit_2 = pop(); cout << digit_2; digit_3 = pop(); cout << digit_3; digit_4 = pop(); cout << digit_4; reversed_number = ((digit_1*1000)+ (digit_2*100)+ (digit_3*10)+ (digit_4*1)); if (reversed_number == number){ cout<<"It is a Palindrome Number!" << endl; } else{ cout<<"It is not a Palindrome Number!" << endl; } return 0; }

2nd Mar 2022, 6:48 PM
Tanzim Ikram Sheikh
Tanzim Ikram Sheikh - avatar
1 Answer
+ 1
You are not understand what return causes and where to use it. so Learn about it detaily, to know your code problem. Make digitSeparator() function void return type and remove all return statements... Like this : void digitSeparator(int num){ number = num; num1 = num / 1000; num_mod = num % 1000; // return num1; num2 = num_mod / 100; num1_mod = num_mod % 100; // return num2; num3 = num1_mod / 10; // return num3; num2_mod = num1_mod % 10; // return num2_mod; }
2nd Mar 2022, 7:58 PM
Jayakrishna 🇼🇳