Created
December 1, 2016 13:35
-
-
Save Xmerr/b0b37226bca7fb14cace645b1933b5d1 to your computer and use it in GitHub Desktop.
Easy Reddit Challenge 290
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Kaprekar_Numbers | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string line; | |
int start, end; | |
List<int> KaprekarNumbers = new List<int>(); | |
Console.WriteLine("Enter Start and End Numbers to test"); | |
Console.WriteLine("Format like so:"); | |
Console.WriteLine("<Start> <End>\n"); | |
line = Console.ReadLine(); | |
Console.Clear() | |
for (start = Convert.ToInt32(line.Substring(0, line.IndexOf(' '))), end = Convert.ToInt32(line.Substring(line.IndexOf(' ') + 1)); | |
start <= end; start++) | |
if (KaprekarTest(start)) | |
Console.Write(start + " "); | |
Console.WriteLine(""); | |
Console.ReadKey(); | |
} | |
static bool KaprekarTest(int num) | |
{ | |
string power = Convert.ToInt64(Math.Pow(num, 2)).ToString(); | |
int num2 = -1; | |
if(power.Length % 2 == 0) | |
num2 = Convert.ToInt32(power.Substring(0, power.Length / 2)) + Convert.ToInt32(power.Substring(power.Length / 2)); | |
else if (power.Length > 1) | |
num2 = Convert.ToInt32(power.Substring(0, (power.Length - 1) / 2)) + Convert.ToInt32(power.Substring((power.Length - 1) / 2)); | |
if (num == num2) | |
return true; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Reddit Easy Challenge 290