C
C#16mo 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.16mo ago
Example : 1256 odd even odd even
TheRanger
TheRanger16mo ago
well there are 2 options in my mind
MODiX
MODiX16mo 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
TheRanger16mo ago
ud have to convert 1256 to string first, then iterate through each character, then convert the char to int
matuda.
matuda.16mo ago
But if I had to put this in a inputdata ?
TheRanger
TheRanger16mo ago
take the string from the input data then
matuda.
matuda.16mo ago
ok thanks But in case i have to transform all this in if else how I do it?
chef drone builder
use if else instead of ? and :
TheRanger
TheRanger16mo ago
its not hard
MODiX
MODiX16mo 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.
chef drone builder
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
TheRanger16mo ago
Dont that's just ugly and bad practice number % 2 == 0 is already a thing can handle every number the computer can handle
chef drone builder
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 User16mo ago
Message Not Public
Sign In & Join Server To View
matuda.
matuda.16mo ago
I solved the problems Thx guys
chef drone builder
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
TheRanger16mo ago
looks like Modulo is faster
Florian Voß
Florian Voß16mo 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
FusedQyou16mo 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ß16mo 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
TheRanger16mo ago
we forgot to benchmark one more thing
MODiX
MODiX16mo 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.
chef drone builder
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
TheRanger16mo ago
bitwise should be faster but release mode optimizes code anyway
FusedQyou
FusedQyou16mo ago
Huh Even then Your attempting to cut corners to make it easier but it just makes it worse
MODiX
MODiX16mo ago
Matuda#8863
I solved the problems
Quoted by
React with ❌ to remove this embed.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
❔ Razor Pages: Temporary Sites like Doodle Surveys with an Share Link?Hey everyone, is it Possible in Razor Pages to create "temporary" Pages?` I would like to create som✅ nuget naming nuances - Microsoft.Extensions.Configuration.Yaml doesn't belong to MicrosoftCan package names be whatever the uploader desires and there's no indication of hierarchy, as far as❔ Creating a base abstract class to ensure a set of derived classes receive similar dependencies?Hi, working on creating a class library and I have a set of classes, let's call them PlainPizza, Pep❔ Using razor pages to generate a list and send to code fileSo I have a table called GenericEvents, and I have various activities that use this generic event toUpdate JSON File with new values without overwriting existing dataHello, So I'm currently developing a Discord bot that stores values such as 'settings' for each gu❔ How to add a list of type of another class in c#?I'm doing a program where I need to store the a list of the Person class in the class of People. A P❔ just a quick help```cs using System; using System.Collections.Generic; using System.Linq; class Program { static✅ Multi-targeting NetFx and NetCore - Targeting wrong version?# History My last post was a bit of a mess so I've come here to help clean it up a bit. I have a p❔ Need help with giant amount of data stored in clipboardI am trying to keep a history of the user's clipboard locally, but if I load into memory a giant cli❔ Dictionary won't be larger than oneHello i'm trying to create a gridmap where every tile has some info, which i use class for storing,