0
How can I use a variable that is inside a function, in my main function?
https://code.sololearn.com/chII62Oh99fE/?ref=app I need the variable to compare it to another variable.
3 Respuestas
+ 3
Declare rev globally , call the palindrome function first then check for ( if rev==n ),
------------------------
int rev=0;
void isPalindrome(int x) {
//complete the function
int temp,r;
temp=x;
while(temp!=0){
r=temp%10;
rev = rev * 10 + r;
temp/=10;
}
}
int main() {
int n;
cin >>n;
isPalindrome(n);
if(rev == n) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}
Also it will be (temp=x not x=temp) ;
+ 1
you need to complete the function,
the function should return a boolean value.
then in main, use the function in if condition passing n as an argument.
if(isPalindrom(n)) {
}
0
the version I was talking about based on the code you posted :
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int temp,r,rev = 0;
temp = x;
while(temp!=0){
r=temp%10;
rev = rev * 10 + r;
temp/=10;
}
return x == rev;
}
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n <<" is NOT a palindrome";
}
return 0;
}