Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C++ identifying a palindrome (output error)

  1. Feb 1, 2012 #1
    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 (Text):

    #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: Feb 1, 2012
  2. jcsd
  3. Feb 1, 2012 #2

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    If I try to compile that I get an obvious error in the second cout statement. If you fix that, I think it will work fine.
     
  4. Feb 1, 2012 #3

    jhae2.718

    User Avatar
    Gold Member

    With syntax highlighting (testing 1..2..3):
    Code (Text):

    [color=#BC7A00]#include <iostream>
    [/color][color=#008000][b]using[/b][/color] std[color=#666666]::[/color]endl;
    [color=#008000][b]using[/b][/color] std[color=#666666]::[/color]cout;
    [color=#008000][b]using[/b][/color] std[color=#666666]::[/color]cin;

    main ()

    {

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

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

    a [color=#666666]=[/color] (number [color=#666666]%[/color] [color=#666666]10[/color]);      
    b [color=#666666]=[/color] (number [color=#666666]%[/color] [color=#666666]100[/color]) [color=#666666]/[/color] [color=#666666]10[/color];
    c [color=#666666]=[/color] (number [color=#666666]%[/color] [color=#666666]1000[/color]) [color=#666666]/[/color] [color=#666666]100[/color];
    d [color=#666666]=[/color] (number [color=#666666]%[/color] [color=#666666]10000[/color]) [color=#666666]/[/color] [color=#666666]1000[/color];
    e [color=#666666]=[/color] (number [color=#666666]%[/color] [color=#666666]100000[/color]) [color=#666666]/[/color] [color=#666666]10000[/color];


     [color=#408080][i]//Compare a to e and b to d. You don't need c
    [/i][/color]
    [color=#008000][b]if[/b][/color] (a[color=#666666]==[/color]e [color=#666666]&&[/color] b[color=#666666]==[/color]d)
        cout[color=#666666]<<[/color][color=#BA2121]"The number is a palindrome![/color][color=#BB6622][b]\n[/b][/color][color=#BA2121]"[/color];
    [color=#008000][b]else[/b][/color]
        cout[color=#666666]<<[/color] [color=#BA2121]"The number is not a palindrome![/color][color=#BB6622][b]\n[/b][/color][color=#BA2121]"[/color];

    [color=#008000][b]return[/b][/color] [color=#666666]0[/color];

    }
     
     
    Last edited: Feb 1, 2012
  5. Feb 2, 2012 #4
    Code (Text):
    #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;}
     
  6. Feb 2, 2012 #5
    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 .
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook