-1

The program below is to find palindromes in products of 2 digit numbers (up to 10*11 for trails).

#include<iostream>
using namespace std;
int res1=0
void palindrome(int mul)
{
    int k,res=0,count=0;
    int p=mul;
    while(mul!=0)
    {
        k=mul%10;
        res=(res*10)+k;
        mul=mul/10;
    }
    if(res==p)
              cout<<res<<"is a palindrome:\n"
        {
        if(res1<res) 
            res1=res;
        }
    }

int main()
{
    int mul,j;
    for(int i=1;i<11;i++)
    {
        for(j=1;j<12;j++)
        {

            mul=i*j;
            if(mul>10)
            palindrome(mul);
        }
        cout<<"\n";
    }

    return 0;
}

The above code works for all that I need except for the largest palindrome.

My algorithm is like

  1. Take the product of each combination
  2. Use a function to check whether the result is palindrome if multiplication res is >10 and print the palindrome.
  3. Check for the largest number by comparing with the previous number.

So I have stored the previous value in res1 variable, and I am comparing the res and res1 variables.

According to my logic it checks lastly (88<99) that is true. Now the problem is how to print the last value stored in res variable?

srija
  • 1
  • 1

2 Answers2

1

Since you have the Global variable res1 that stores the value of the palindrome, consider adding a cout to output this value: cout<<"Last value: "<<res1<<endl; before you return 0.

Of course, I'd reccommend that your palindrome function instead accept and return res1 to avoid having the global variable and some other semantic type things... but those are different issues which may not matter to you.

Bob2Chiv
  • 1,858
  • 2
  • 19
  • 29
0

I have written a code to check whether the input number by user is palindrome or not. The code also returns the reverse of given number. Code is as follows:

#include<iostream.h>
#include<conio.h>

class palindrome
{
    int a;
    int f;
    int b;
    int l;
    int c;
    int count;
public:
    void getdata();
    void counting();
    void reverse();
    void display();
};

void palindrome::getdata()
{
    cout<<"Enter any number having atleast 2 digits";
    cin>>a;
    b=a;
    c=a;
}

void palindrome::counting()
{
    int count=1;
    f=1;
    do
    {
        a=a/10;
        count++;
    }while(a/10!=0);

    for(int k=1;k<count;k++)
    {
        f=f*10;
    }
}

void palindrome::reverse()
{
    l=0;
    for(int i=1;i<=count;i++)
    {
        int k=b%10;
        k=k*f;
        f=f/10;
        l=l+k;
        b=b/10;
    }
    cout<<"\nThe reverse of the number is"<<l;

}

void palindrome::display()
{
    if(l==c)
    {
        cout<<"\nThe number is palindrome";
    }
    else
        cout<<"\nThe number is not palindrome";
}

void main()
{
    palindrome a; 
    clrscr();
    a.getdata();
    a.counting();
    a.reverse();
    a.display();
    getch();
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Deepeshkumar
  • 395
  • 3
  • 13