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 Réponse
+ 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.