- 2
Who has answer on Palindrome numbers on c++ please help me
12 Antworten
+ 1
try to change the number to string and check if the string is symmetrical like num[0] == num[n-1], num[1] == num[n-2] and so on ....
+ 18
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
int num, digit, rev = 0;
num = x;
do { digit = x % 10; rev = (rev * 10) + digit; x = x / 10; } while (x != 0);
if (num == rev) { 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;
}
+ 4
/*Hi!
I have made an easy one with explanation!
Here it is:*/
#include <iostream>
using namespace std;
int main() {
int a;
cin>>a;
int b=a;
int c=a;
while (a>9||0) { /*separates the last
digit*/
a=a%10;
}
while (b>9||0) { /*separates the first
digit*/
b=b/10;
}
if (a==b) { /*compares them if
they are equal or not*/
cout<<c<<" is a palindrome"<<endl;
}
else {
cout<<c<<" is NOT a palindrome"<<endl;
}
}
/*and if they are equal it prints that the number is a palindrome, if not then it prints that the number is NOT a palindrome*/
/*Don't change anything if you don't know how it works and if you want it to run properly.*/
+ 4
If you need
#include <iostream>
using namespace std;
//function to check
bool isPalindrome(int x) {
int h = x, i = 1, b = 0;
do {
h /= 10;
i *= 10;
} while (h > 10);
h = x;
while (i > 0) {
b = b + (h %10) * i;
i /= 10;
h /= 10; }
return x == b;
}
//run code
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}
0
Show your attempt first.
0
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int y = 1, r = 0, z = x;
bool result;
do {
r = r*10;
r += x%10;
y *= 10;
x = z / y;
}while (y <= z);
if (r == z) result = true;
else result = false;
return result;
}
int main() {
int n;
cin>> n;
if(isPalindrome(n)) {
cout<< n<< " is a palindrome";
}
else {
cout<< n<< " is NOT a palindrome";
}
return 0;
}
Enjoy ☺️
0
Sololearn need to fix their tests in this code project, because only Teo and Фирдавс answers are correct. Try to input 1 and 1231. Sololearn have 6 tests, but all of these tests miss problem with input like 1 and 1231. I will try to contact Sololearn to share this :)
- 1
Try this out
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
cin>>n;
originalInteger = n;
while( n!=0 )
{
remainder = n%10; reversedInteger = reversedInteger*10 + remainder; n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger) cout<<originalInteger<<" is a palindrome";
else cout<<originalInteger<<" is NOT a palindrome";
return 0;
}
- 1
#include <iostream>
using namespace std;
bool isPalindrome(int reverse, int check) {
bool status = true;
if (reverse != check) {
status = false;
}
return status;
}
int main() {
int n, reverse = 0, rem;
cin >> n;
int check = n;
while(n != 0){
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
if(isPalindrome(reverse, check) == 1) {
cout << reverse <<" is a palindrome";
} else {
cout << check <<" is NOT a palindrome";
}
return 0;
}
- 3
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int n,s=0,r;
x=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
}
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}
- 13
A palindromic number is a number (such as 626) that remains the same when its digits are reversed.
Write a function that returns true if a given number is a palindrome, and false, if it is not.
Complete the given function, so that the code in main works and results in the expected output.
Sample Input:
13431
Sample Output:
13431 is a palindrome