6253 | 2023-11-09 14:20:39 | RedStar | Diana and Numbers | csharp | Forditási hiba |
// See https://aka.ms/new-console-template for more information
long userInput = -1;
// Get the user input
while (userInput < 0)
{
Console.WriteLine("Insert a natural number: ");
// If the user input is in incorrect format, continue the cycle
try
{
userInput = long.Parse(Console.ReadLine()!);
}
catch (Exception e)
{
userInput = -1;
}
}
string numberAsString = userInput.ToString();
while (true)
{
// If number has leading zeroes, remove them
while (numberAsString.Length > 0 && numberAsString[0] == '0')
{
numberAsString = numberAsString.Remove(0, 1);
}
// If there is no number inside the variable, the user given number is impossible to make dividable by 3.
if (numberAsString.Length == 0)
{
numberAsString = "-1";
break;
}
// If number is dividable by 3, end the main cycle
if (long.Parse(numberAsString) % 3 == 0)
{
break;
}
// Find the index of the first digit that's not dividable by 3.
int index = -1;
for (int i = 0; i < numberAsString.Length; i++)
{
if (numberAsString[i] % 3 != 0)
{
index = i;
break;
}
}
// Remove the digit from the whole number
numberAsString = numberAsString.Remove(index, 1);
}
Console.WriteLine(numberAsString);
exit status 1
Compilation failed: 1 error(s), 0 warnings