can anyone give an idea for how to write a code that said if a nubmer that was obtained by ReadLIne, has diffrent digits?
thankes for helping.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 5, 2011, 7:44 AM
How I missed that I don't know!
Matan did start a separate thread where he asked for a solution to this using both integers and doubles but it then got mysteriously deleted.
Anyway, doubles are tricky given that they usually have an approximate representation within the machine. Consequently, when you start multiplying or dividing by 10, some of the lower order digits can change and mess up the program. The best solution I've been able to come up with is to convert the double to decimal which crystallizes the initial digits and after that arithmetic is exact:
using System;
using System.Numeric;
class Program
{
static void Main()
{
bool[] used = new bool[10]; // all false by default
Console.Write("Enter a number : ");
double number = double.Parse(Console.ReadLine());
decimal num = (decimal)Math.Abs(number); // convert to decimal and ignore sign
bool different = true;
while (num != Decimal.Truncate(num)) num *= 10m; // move all digits to right of dp to left of dp
Console.WriteLine("The digits are\n");
while (num > 0m)
{
int digit = (int)(num % 10m);
num /= 10m;
num = Decimal.Truncate(num);
Console.WriteLine(digit); // print out digits so we can see what's happening
if (used[digit])
{
different = false;
break;
}
else
{
used[digit] = true;
}
}
Console.WriteLine();
if (different)
{
Console.WriteLine("Digits are different");
}
else
{
Console.WriteLine("Digits are not different");
}
Console.ReadKey();
}
}
Roy SPosted Nov 4, 2011, 5:34 PM
I just changed Vulpes code a bit. The digits are now obtained by dividing the number by 10 and taking the rest of that division as the digit.
EDIT: Realised it was a bit silly to actually store the digits in a list/array...
using System;
class Program
{
static void Main()
{
bool[] used = new bool[10]; // all false by default
Console.Write("Enter a number : ");
int number = int.Parse(Console.ReadLine());
bool different = true;
while (number > 0)
{
int digit = number % 10;
number /= 10;
if (used[digit])
{
different = false;
break;
}
else
{
used[digit] = true;
}
}
if (different)
{
Console.WriteLine("Digits are different");
}
else
{
Console.WriteLine("Digits are not different");
}
Console.ReadKey();
}
}
VulpesPosted Nov 4, 2011, 9:53 AM
matan sharkPosted Nov 4, 2011, 9:25 AM
VulpesPosted Nov 4, 2011, 9:21 AM
VulpesPosted Nov 4, 2011, 8:29 AM
Do you have anything in mind?
matan sharkPosted Nov 4, 2011, 8:01 AM
VulpesPosted Nov 4, 2011, 7:52 AM
VulpesPosted Nov 4, 2011, 7:23 AM
Here's a quick example of this technique (needs VS 2008 or later):
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Write("Enter a number : ");
string number = Console.ReadLine();
HashSet
if (hs.Count == number.Length)
{
Console.WriteLine("Digits are different");
}
else
{
Console.WriteLine("Digits are not different");
}
Console.ReadKey();
}
}