Largest Palindrome
Problem 4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
using System; using System.Collections.Generic; using System.IO; using System.Linq; public class Solution { static void Main(String[] args) { int t = Convert.ToInt32(Console.ReadLine()); for(int a0 = 0; a0 < t; a0++){ int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(ProductOfNumber(n)); } } static long ProductOfNumber(int n) { long num=0; long max =0; for(var i=100; i<=999; i++) { for( var j=i; j<=999; j++) { num = i * j; if (num > n) { break; } if (IsPalindrome(num)) { if (num > max) { max = num; } } } } return max; } static bool IsPalindrome(long n) { return n.ToString() == Reverse(n.ToString()); } static string Reverse(string str) { return new string(str.ToCharArray().Reverse().ToArray()); } }
Live Code on Paiza.io
Comments
Post a Comment