- 3
palindromic number
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
21 Réponses
+ 53
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
+ 3
Thank you very much🤗❤
+ 2
#include <iostream>
//#include <conio.h>
using namespace std;
int main()
{
int Input_Num;
int Orignal_Num;
int Reverse_Num;
int Remainder=0;
cin>>Input_Num;
Orignal_Num=Input_Num;
//int lengthCount = 0;
for(; Input_Num != 0;)
{
Reverse_Num=Input_Num%10;
Input_Num /= 10;
Remainder=Remainder*10+Reverse_Num;
Reverse_Num=Remainder;
}
if(Orignal_Num==Reverse_Num) {
cout <<Orignal_Num<<" is a palindrome";
}
else {
cout <<Orignal_Num<<" is NOT a palindrome";
}
return 0;
}
+ 1
Unfortunately, I did not find the solution, so I shared the problem here so that someone can guide me to the solution, but I think I will use the "string" to cut or edit.
+ 1
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
+ 1
#include <iostream>
using namespace std;
int revDigit(int y)
{
int rem,rev=0;
while(y>0)
{
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x)
{
if(x==revDigit(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;
}
+ 1
Let's break it down step by step:
1. `#include <iostream>`: This is a preprocessor directive that includes the iostream library which provides input and output stream functionality in C++. It allows the use of standard input and output operations like cout and cin.
2. `using namespace std;`: This line is used to avoid typing 'std::' before certain elements from the standard library such as cout and cin. By using the 'using namespace std;' statement you can directly use these elements without specifying the namespace.
3. `int revDigit(int y)`: This is the definition of a function named 'revDigit which takes an integer 'y' as a parameter and returns an integer.
4. `int rem rev = 0;`: This line declares two integer variables named 'rem' and 'rev and initializes 'rev' to 0.
5. `while (y > 0) { ... }`: This is a while loop that continues to execute the code inside the loop as long as the condition 'y > 0' is true.
6. Inside the while loop:
- `rem = y % 10;`: This line calculates the remainder of 'y' when divided by 10 and assigns it to the 'rem' variable.
- `rev = rev * 10 + rem;`: This line updates the 'rev' variable by appending the 'rem' value at the end.
- `y /= 10;`: This line divides 'y' by 10 and assigns the result back to 'y effectively removing the last digit from 'y'.
7. After the while loop `return rev;` is used to return the final value of 'rev' from the 'revDigit' function.
In summary the 'revDigit' function takes an integer as input and reverses its digits. For example if you pass 1234 to this function it will return 4321.
0
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
0
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
0
#include <iostream>
using namespace std;
int revdigit(int y){
int rem, rev=0;
while(y>0){
rem = y%10;
rev = rev*10+rem;
y = y/10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revdigit(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;
}
0
#include <iostream>
using namespace std;
int revdigt(int z){
int digit,rev=0;
while (z > 0)
{
digit = z % 10;
rev = (rev * 10) + digit;
z = z/ 10;
}
return rev;
}
bool isPalindrome(int x) {
//complete the function
if (x == revdigt(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;
}
0
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
0
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int rev, val;
val = x;
while(x > 0){
rev = rev * 10 + x % 10;
x = x / 10;
}
if (rev == val){
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;
}
GOOGLE is your best friend in CODING!!
0
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
Good Luck
0
Can someone explain to me how the entire code works please? This is how I understand it:
starting from main() the isPalindrome(n) function is called passing the n variable (input) to that function. Then within that function does variable n become x within the isPalindrome function? Then x is compared to revDigit while also calling the revDigit function? I want to make sure I understand this correctly,
Thank you!
0
#include <iostream>
using namespace std;
int revDigit(int y){
int rem,rev=0;
while(y>0){
rem=y%10;
rev=rev*10+rem;
y/=10;
}return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revDigit(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;
}
0
#include <iostream>
using namespace std;
int revers(int y){
int rev=0 , number;
while(y>0){
rev*=10;
number=y%10;
rev+=number;
y/=10;
}
return rev;
}
bool isPalindrome(int x) {
//complete the function
if(x==revers(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;
}
0
#include <algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str, temp, str2;
getline (cin, str);
str2=str;
reverse(str.begin(), str.end());
temp=str;
temp.insert(temp.end(),' ');
int n = temp.length();
int j = 0;
for (int i = 0; i < n; i++) {
if (temp[i] == ' ') {
reverse(temp.begin() + j,
temp.begin() + i);
j = i + 1;
}
}
reverse(temp.begin(), temp.end());
if(str==str2) {
cout <<str2<<" is a palindrome";
}
else {
cout << str2<<" is NOT a palindrome";
}
return 0;
}
0
took a while to figure it out...
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int backward;
if (x != 0 && x % 10 == 0 || x < 0)
return false;
while(x > backward){
backward = backward * 10 + x % 10;
x = x / 10;
}
if(x == backward)
{
return true;
}
if(x == backward / 10)
{
return true;
}
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;
}
- 1
hello guys your code it's doesn't works because 8888 is not palindrome but i right in number 1 and 5,6