C++ identifying a palindrome (output error)

  • Context: Comp Sci 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    C++ Error
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 3K views
mr.me
Messages
49
Reaction score
0
I had a homework assignment that wanted me to write a function that would determine whether a five digit integer was a palindrome or not

When I run the program I enter the palindrome there is no output for my if/else statement. What did I do wrong? Code Below

Code:
#include <iostream>
using std::endl;
using std::cout;
using std::cin;

main ()

{

int number; 
int a,b,c,d,e;

cout<< "I will take a digit five digit interger and indentify whether or not it is palindrome...";
cout<< "Please enter the number you want to check!\n"endl;
cin>> number;    // a = (number % 10);      
b = (number % 100) / 10;
c = (number % 1000) / 100;
d = (number % 10000) / 1000;
e = (number % 100000) / 10000; //Compare a to e and b to d. You don't need c

if (a==e && b==d)
    cout<<"The number is a palindrome!\n";
else 
    cout<< "The number is not a palindrome!\n";

return 0;

}
 
Last edited:
Physics news on Phys.org
With syntax highlighting (testing 1..2..3):
Code:
#include <iostream>
[/color][b]using[/b][/color] std::[/color]endl;
[b]using[/b][/color] std::[/color]cout;
[b]using[/b][/color] std::[/color]cin;

main ()

{

int[/color] number; 
int[/color] a,b,c,d,e;

cout<<[/color] "I will take a digit five digit interger and indentify whether or not it is palindrome..."[/color];
cout<<[/color] "Please enter the number you want to check![/color][b]\n[/b][/color]"[/color]endl; // << before endl here!
cin>>[/color] number;    [i]// 
[/i][/color]

a =[/color] (number %[/color] 10[/color]);      
b =[/color] (number %[/color] 100[/color]) /[/color] 10[/color];
c =[/color] (number %[/color] 1000[/color]) /[/color] 100[/color];
d =[/color] (number %[/color] 10000[/color]) /[/color] 1000[/color];
e =[/color] (number %[/color] 100000[/color]) /[/color] 10000[/color]; [i]//Compare a to e and b to d. You don't need c
[/i][/color]
[b]if[/b][/color] (a==[/color]e &&[/color] b==[/color]d)
    cout<<[/color]"The number is a palindrome![/color][b]\n[/b][/color]"[/color];
[b]else[/b][/color] 
    cout<<[/color] "The number is not a palindrome![/color][b]\n[/b][/color]"[/color];

[b]return[/b][/color] 0[/color];

}
 
Last edited:
Code:
#include <iostream>
using namespace std;
 
int isp(int j){
        int p;
        p=j%10; j /= 10;
        p=p*10 + j%10;
        return p == j/100; }
                
int main(void) {
        for(int i=10000; i<=99999; i++ ) 
                if( isp(i) ) 
                        printf("%5d\n", i); 
        return 0;}
 
Thanks

I never got an error message and couldn't get it to run proper so it tried using a different compiler
and fixed the error .