0
I want it to show the largest palindromic number...plzz help
#include <iostream> using namespace std; int palindrome(int k); int main(){ for(int x=100;x<999;x++){ for(int y=100;y<999;y++){ int p=x*y; cout<<palindrome(p)<<endl; } } } int palindrome(int k){ int reversed=0; int n=k; while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; } return reversed==n; }
1 Answer
+ 4
To find the largest palindrome create a variable inside main called 'prev_p', and replace the cout statement in the for loop with :
if( p>prev_p && palindrome(p))
prev_p = p;
Now after the end of the for loops, prev_p will have the largest value.
Alternatively, you can run the i loop from 999 to 100 (in dec order), the j loop from i to 100, and compare using the same approach.
Also, you can decrease the limit of the i loop to [999,900] as the palindrome is likely to be a product of numbers from this range.