+ 1
C++ palindrome number
How did i solve this program please give me the idea
10 Respuestas
+ 8
vijayalakshmi T.G. Hii. Here is your code.
#include <iostream>
using namespace std;
int main() {
int a,b,s=0;
cout << "Hii. Enter a number: ";
cin >> a;
b=a;
while(a!=0){
s=s*10+a%10;
a/=10;
}
if(s==b)
cout << "Palindrome.";
else
cout << "Not Palindrome."
return 0;
}
+ 2
U need to write here if condition after while
if (reverse_num==digit)
+ 2
Martin Taylor
I was unaware that it had to be strictly a function, my apologies XD. Anyway, it is easy to solve:
bool ispalindrome (int num) {
int dig, aux = 0, reves;
aux = num;
for (reves = 0; aux! = 0;)
{dig = aux% 10;
aux = aux / 10;
reves = reves * 10 + dig;
}
if (reves == num) return true;
else return false;
}
+ 2
This is my code, for checking given number is palindrome number or not
#include <iostream>
using namespace std;
int main() {
int n,temp,rev=0,d;
cin >>n;
temp=n;
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(temp==rev) {
cout <<temp<<" is a palindrome";
}
else {
cout << temp<<" is NOT a palindrome";
}
return 0;
}
This correct for the checking palindrome number or not
+ 1
Show us the code so we can help
+ 1
vijayalakshmi T.G. Show as your code attempt.
+ 1
Although you can do it by converting the number into a string so as not to apply math, here is the math way to solve it
https://code.sololearn.com/c0hIFL9T3slC/?ref=app
+ 1
Ok thank you all👍
0
bool ispalindrome (int x){
do {
digit=x%10;
rev=(rev*10)+digti;
x=x/10;
}while (x!=0);
cout <<"The reverse of the number is:"<<rev<<endl;
0
Try my code it works::
#include <iostream>
using namespace std;
void isPalindrome(int x) {
//complete the function
int n = 0;
int last;
int w = x;
while (x >0){
last = x%10;
n = n*10 + last;
x = x/10;
}
if (n == w){
cout <<w<<" is a palindrome";
}
else {
cout << w<<" is NOT a palindrome";
}
}
int main() {
int n;
cin >>n;
isPalindrome(n);
return 0;
}