Hi all,

My program is proving Kaprekar's Constant - 6174.

To do this I will need to store values exactly how they come out without losing any preceding 0's which int has a habit of cutting off.

For example:
Number to store exactly: 0147
Int stores: 147

A googling session has left me short and so i wondered is it actually possible to stop an int from cutting off preceding zeros or will I need to use strings?

Cheers,

Mike

I would look into the modulus operator using it something like below.

``````using System;

namespace testit
{
class MainClass
{
public static void Main (string[] args)
{
UInt64 x = 0;

Console.Write("Enter a four digit number->");
Console.WriteLine(reverse_it(x));
}

public static UInt64 reverse_it(UInt64 num)
{
UInt64 reverse = 0;

byte [] ans = new byte[]{0,0,0,0};

ans[3] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[2] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[1] = (byte)(num % 10);
num = (num -= num % 10) / 10;
ans[0] = (byte)(num % 10);
num = (num -= num % 10) / 10;

for (UInt64 i = 3; i > 0; --i)
{
reverse += ans[i];
reverse *= 10;
}
reverse += ans[0];
return reverse;

}
}
}``````

If it expects four digits, then its input cannot be in the form of an int. It must be a string. The number of decimal digits has no meaning in the 'int' type - it's not natively an decimal type. (It's actually a 32-digit binary type. It's just that C# lets you specify numeric constants as decimal text for convenience - nobody wants to type a 32-digit binary number...)

You say your "my method accepts integers onli", but that contradicts what you say here: "it accepts only 4 digits in the Year field"

One of those statements must be untrue. The 'int' type in C# and also Java (presumably Java is somehow involved if you have a JAR file) doesn't have any mechanism for representing leading zeroes.

So if your method only accepts integers, then by definition it can't care about leading zeroes because the 'int' type doesn't actually support that concept. And if your method accepts only 4 digits, then by defintion the data you're passing it can't be an 'int' because if it was, it wouldn't be able to tell whether there was a leading digit or not.

As far as the 'int' type is concerned, 999, 0999, 0000999, 0x3E7, and 0x00003E7 are all exactly the same thing. These different textual representations are completely indistingiushable in an 'int' because they all have the same binary representation: 0000001111100111.

Note that the leading zeroes are always present in the underlying representation. 'int' is 32-bits. If the number you wish to represnet doesn't need all 32 bits, the leading digits are all 0. That's why you can't add them - they're already there, whether you specified them or not.

Since you are going to have to sort the digits that make up the numbers it's easier to convert them to strings, manipulate the digits then convert them back to numbers. With the conversion to string you can specify that it have leading zeros, so that will make it easy.

Mitja i do wonder if you actually read the post as i didnt mention a JAR file or years anywhere in my post. Lol.

As for Momerath thank you for your input I will use that method :)