We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void kaprekarNumbers(int p, int q)
{
List<int> numbers = new List<int>();
for (int i = p; i <= q; i++)
{
var digits = i.ToString().ToCharArray().Count();
var square = Math.Pow(i, 2);
var square_digits = square.ToString().ToCharArray().Count();
int left = 0;
int right = 0;
if (square_digits > 1)
{
int digitsForLeft = square_digits - digits;
left = Convert.ToInt32(square.ToString().Substring(0, digitsForLeft));
right = Convert.ToInt32(square.ToString().Substring(digitsForLeft));
}
else
{
right = (int)square;
}
if (left + right == i)
numbers.Add(i);
}
Console.WriteLine(numbers.Count > 0 ? string.Join(" ", numbers) : "INVALID RANGE");
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
Hi, this is my solution in C# 🐿️