C
C#Despy()

❔ help with an exercise

I've encountered a weird problem that idk how to fix. Say we've got a string "31131123521" how do i make it so each number of the string is multiplied by one another so the string we have got will be equal to 3 * 1 * 1 * 3 * 1 * 1 * 2 * 3 * 5 * 2 * 1 (this is equal to 540). I have trouble finding a patter or solution to this when it seems very simple. Given a string i just want to multiply each number inside of it
A
Angius398d ago
First, you'll need to get an array of numbers from this string Use the fact that a string already is an array of chars And that a method char.GetNumericValue() method exists You'll need to do the multiplications in a loop And keep the result outside of the loop
D
Despy()398d ago
mind writing some quick code for this ; ; ? heres the structure
string wholeString = Console.ReadLine();
string multiplicationNumbers = "";
foreach (var letter in wholeString)
{
if (char.IsDigit(letter))
{
multiplicationNumbers += letter;
}
}
Console.WriteLine(multiplicationNumbers);
for (int i = 0; i < multiplicationNumbers.Length; i++)
{

}
string wholeString = Console.ReadLine();
string multiplicationNumbers = "";
foreach (var letter in wholeString)
{
if (char.IsDigit(letter))
{
multiplicationNumbers += letter;
}
}
Console.WriteLine(multiplicationNumbers);
for (int i = 0; i < multiplicationNumbers.Length; i++)
{

}
i dont know how to like, i even tried using chatGPT for this and still nothing i dont know in what pattern to multiply the numbers
A
Angius398d ago
*
D
Despy()398d ago
a huge number in the millions always comes up
S
Shinigami398d ago
There's no pattern
A
Angius398d ago
That's how you multiply
D
Despy()398d ago
when in fact the answer is 540 i know okay can yall get my code and do it then
A
Angius398d ago
Nope Not doing your homework for you
S
Shinigami398d ago
We can tell how to do it You could really google what @Angius just told How to make a string into char array?
D
Despy()398d ago
thats not my problem
S
Shinigami398d ago
Then?
D
Despy()398d ago
i know to convert chars into ints and such. the problem is when i try doing it with code the answer comes up as smth ludicrous in the millions. keep in mind this is the string "31131123521"
A
Angius398d ago
Show the code, then
D
Despy()398d ago
when in fact the answer to 3 * 1 * 1 * 3 * 1 * 1 * 2 * 3 * 5 * 2 * 1 is 540
A
Angius398d ago
Show your code that converts the string into an array of ints
S
Shinigami398d ago
Yes If the logic for how you're multiplying is flawed then it'd obv show huge number
D
Despy()398d ago
it is flawed thats what im asking for ; ; the logic itself
A
Angius398d ago
Well, show the code
D
Despy()398d ago
sec
S
Shinigami398d ago
Show us what you've done, for us to understand where u went wrong
D
Despy()398d ago
sorry i had to do smth
string str = "31131123521";
int result = 1;
foreach (char c in str)
{
int digit = int.Parse(c.ToString());
result *= digit;
}
Console.WriteLine(result);
string str = "31131123521";
int result = 1;
foreach (char c in str)
{
int digit = int.Parse(c.ToString());
result *= digit;
}
Console.WriteLine(result);
A
Angius398d ago
Seems to work just fine
A
Angius398d ago
D
Despy()398d ago
nevermind it was my mistake
A
Accord397d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What❔ XMLAttributeCollection -> Dictionary with LINQ?It's a confusing class. It only allows turning it into a Queryable, but I have no experience with th❔ MS SQL Reporting Server URL Being RedirectedWe have an application from a vendor that was written in C# and we recently upgraded the MS SQL data❔ dataset memory leakI found memory leak and I don't understand how to fix it Test case: ```cpp public class MemoryLeaksT❔ WebAPI .NET 7.0 - Encrypt connectionstring in appsettings.jsonguys, I'm writing some WebAPI using .NET 7.0, and I'd like to secure the connectionstring with encry❔ how to read strings correctly with System.Data.SQLitei m trying read turkish characters from database but i can't see correctly❔ Blazor server app, base url / base page to set environmentHi 🙂 I have a Blazor Server app, where I would like to use some dynamic baseurl. Say instead of my❔ C# Console Application -its only writing the same random string, how can i make it do different ones?❔ Rapid prototypingDo you feel there is such a thing as rapid prototyping or mvp development where you do things differ❔ HotChocolate with IQueryable, apply a required filter on the ef entityBasically, I'm trying to find a way to configure an `IObjectFieldDescriptor` by adding a required ar❔ Dev/prod databseHello, Me and a friend wants to create a webb-app project with a react frontend and C# backend. ❔ Book recommendation to learn c#/dotnetI've been programming in python for a little under two years. I am starting a new job in c#. Whats a❔ Use `this` inside method decoratorTried to follow https://stackoverflow.com/a/2966758 ```cs [AttributeUsage(AttributeTargets.Metho✅ How can I make VSCode auto add semicolons to statements on save?I'm coming from the world of Javascript and Typescript. There, you can just use Prettier to automati❔ Find common substring at start of every string in listI have a list of Windows path strings and I want to determine the common beginning shared by all of ❔ Code is completely skipping over an areaI'm writing a script for a game where if you don't press Spacebar within 4 seconds, the number of fa❔ Registering an extracted appx package```csharp public static void RegisterMinecraftAppxPackage(string appxPackagePath) { try { ✅ Exception Handling for NRE without modifying every call.I have a ReadData function which reads the memory of a process. If the process exits or crashes duri✅ please help idk what to do or what i didSeverity Code Description Project File Line Suppression State Error Project❔ Better user experience inputting commandsHow would I get input from a user, the fancy way? What I mean is that for example when a user types