C
C#2y ago
matuda.

✅ If else statement problem

Even or odd number I got a problem to write an app to specify every digit from a number of 4 digits if its even or odd and I have no idea how to specify in code
27 Replies
matuda.
matuda.OP2y ago
Example : 1256 odd even odd even
TheRanger
TheRanger2y ago
well there are 2 options in my mind
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
foreach(char digit in "1256".ToString())
{
int number = (int)Char.GetNumericValue(digit);
Console.WriteLine(number % 2 == 0 ? "even" : "odd");
}
foreach(char digit in "1256".ToString())
{
int number = (int)Char.GetNumericValue(digit);
Console.WriteLine(number % 2 == 0 ? "even" : "odd");
}
Console Output
odd
even
odd
even
odd
even
odd
even
Compile: 673.014ms | Execution: 45.053ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
ud have to convert 1256 to string first, then iterate through each character, then convert the char to int
matuda.
matuda.OP2y ago
But if I had to put this in a inputdata ?
TheRanger
TheRanger2y ago
take the string from the input data then
matuda.
matuda.OP2y ago
ok thanks But in case i have to transform all this in if else how I do it?
anita
anita2y ago
use if else instead of ? and :
TheRanger
TheRanger2y ago
its not hard
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
string input = "1256";
foreach(char digit in input)
{
int number = (int)Char.GetNumericValue(digit);
if (number % 2 == 0)
{
Console.WriteLine("even");
}
else
{
Console.WriteLine("odd");
}

}
string input = "1256";
foreach(char digit in input)
{
int number = (int)Char.GetNumericValue(digit);
if (number % 2 == 0)
{
Console.WriteLine("even");
}
else
{
Console.WriteLine("odd");
}

}
Console Output
odd
even
odd
even
odd
even
odd
even
Compile: 570.023ms | Execution: 85.246ms | React with ❌ to remove this embed.
anita
anita2y ago
this is better i think:
if (input == 0) return "Even";
if (input == 1) return "Odd";
if (input == 2) return "Even";
if (input == 3) return "Odd";
if (input == 4) return "Even";
if (input == 5) return "Odd";
if (input == 6) return "Even";
if (input == 7) return "Odd";
if (input == 8) return "Even";
if (input == 9) return "Odd";
if (input == 0) return "Even";
if (input == 1) return "Odd";
if (input == 2) return "Even";
if (input == 3) return "Odd";
if (input == 4) return "Even";
if (input == 5) return "Odd";
if (input == 6) return "Even";
if (input == 7) return "Odd";
if (input == 8) return "Even";
if (input == 9) return "Odd";
TheRanger
TheRanger2y ago
Dont that's just ugly and bad practice number % 2 == 0 is already a thing can handle every number the computer can handle
anita
anita2y ago
ok, you are right, I should have used a switch. I will test performance test it against %. I think it could be faster. give me a minute
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
matuda.
matuda.OP2y ago
I solved the problems Thx guys
anita
anita2y ago
ok, i tested it with benchmark.net, this is the result: | Method | Input | Mean | Error | StdDev | Median | |------- |------ |----------:|----------:|----------:|----------:| | Switch | 0 | 1.7937 ns | 0.1157 ns | 0.2539 ns | 1.6716 ns | | IfElse | 0 | 1.4684 ns | 0.0451 ns | 0.0377 ns | 1.4630 ns | | Modulo | 0 | 1.1612 ns | 0.0829 ns | 0.1315 ns | 1.1269 ns |
TheRanger
TheRanger2y ago
looks like Modulo is faster
Florian Voß
Florian Voß2y ago
ehhhm both the switch and the ifelse approach should be using a modulo operator... What you actually wanted to benchmark I guess is ifelse vs. switch vs. ternary operator all these use modulo
FusedQyou
FusedQyou2y ago
Lol This makes me think of that IsEven/IsOdd package People not knowing what the modulo operator is Even if your approach/the switch approach was faster, it makes literally 0 sense to do anything but a modulo operator But please have fun scaling your if-else statements to support up to 10 digits when it's asked, if not more
Florian Voß
Florian Voß2y ago
as I've pointed out, a good switch statement solution would contain the modulu operator too, just like an ifelse or a ternary operator solution.
TheRanger
TheRanger2y ago
we forgot to benchmark one more thing
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
string input = "1256";
foreach(char digit in input)
{
int number = (int)Char.GetNumericValue(digit);
if ((number & 1) == 0)
{
Console.WriteLine("even");
}
else
{
Console.WriteLine("odd");
}

}
string input = "1256";
foreach(char digit in input)
{
int number = (int)Char.GetNumericValue(digit);
if ((number & 1) == 0)
{
Console.WriteLine("even");
}
else
{
Console.WriteLine("odd");
}

}
Console Output
odd
even
odd
even
odd
even
odd
even
Compile: 646.896ms | Execution: 89.763ms | React with ❌ to remove this embed.
anita
anita2y ago
The requirement was clear, we only care about single digits. I think if the switch would be faster and performance really really really matters, then it would make sense.
TheRanger
TheRanger2y ago
bitwise should be faster but release mode optimizes code anyway
FusedQyou
FusedQyou2y ago
Huh Even then Your attempting to cut corners to make it easier but it just makes it worse
MODiX
MODiX2y ago
Matuda#8863
I solved the problems
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server