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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 3:23 AM EDT
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();
}
}
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 3:52 AM EDT
matan shark5 reputationPosted Nov 4, 2011, 4:01 AM EDT
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 4:29 AM EDT
Do you have anything in mind?
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 5:21 AM EDT
matan shark5 reputationPosted Nov 4, 2011, 5:25 AM EDT
VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 5:53 AM EDT
Roy S194 reputationPosted Nov 4, 2011, 1:34 PM EDT
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();
}
}
VulpesRank #55 · 29.2K reputationPosted Nov 5, 2011, 3:44 AM EDT
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();
}
}