max element of numbers
I calculate palindrome and i'm need found max element of this list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
using namespace std;
bool checkPalim(int);
struct myclass {
void operator()(int i) { cout << " " << i; }
}myobject;
int main(int argc, char**argv)
{
int max = 0, pal = 0;
for (int i = 999; i > 99; --i)
{
for (int j = 999; j >99; --j)
{
pal = i*j;
if (checkPalim(pal))
cout << pal << "\n";
}
/*if (pal > max)
{
max = pal;
cout << "\n\nThe large " << max;
}*/
}
return 0;
}
bool checkPalim(int num)
{
bool condition = true;
int a=0, b=0, c;
c = num;
while (num)
{
a = num % 10;
num /= 10;
b = b * 10 + a;
}
if (b == c)
{
condition = true;
}
else
condition = false;
return condition;
}
|
struct myclass serves no purpose in this program and should be removed, ditto for variable MAX
to find the max element have another variable, int temp, that maintains a running log of the max element and prints it out at the end:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
using namespace std;
bool checkPalim(int);
int main(int argc, char**argv)
{
int temp = 0, pal = 0;
for (int i = 999; i > 99; --i)
{
for (int j = 999; j >99; --j)
{
pal = i*j;
if (checkPalim(pal))
{
if (pal > temp)
{
temp = pal;
};
}
}
}
cout << "Max palindrome: "<< temp <<"\n";
}
bool checkPalim(int num)
{
bool condition = true;
int a=0, b=0, c;
c = num;
while (num)
{
a = num % 10;
b = b * 10 + a;
num /= 10;
}
if (b == c)
{
condition = true;
}
else
{
condition = false;
}
return condition;
}
|
Topic archived. No new replies allowed.