C
C#2y ago
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
25 Replies
Angius
Angius2y 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
Despy()
Despy()OP2y 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
Angius
Angius2y ago
*
Despy()
Despy()OP2y ago
a huge number in the millions always comes up
Shinigami
Shinigami2y ago
There's no pattern
Angius
Angius2y ago
That's how you multiply
Despy()
Despy()OP2y ago
when in fact the answer is 540 i know okay can yall get my code and do it then
Angius
Angius2y ago
Nope Not doing your homework for you
Shinigami
Shinigami2y ago
We can tell how to do it You could really google what @Angius just told How to make a string into char array?
Despy()
Despy()OP2y ago
thats not my problem
Shinigami
Shinigami2y ago
Then?
Despy()
Despy()OP2y 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"
Angius
Angius2y ago
Show the code, then
Despy()
Despy()OP2y ago
when in fact the answer to 3 * 1 * 1 * 3 * 1 * 1 * 2 * 3 * 5 * 2 * 1 is 540
Angius
Angius2y ago
Show your code that converts the string into an array of ints
Shinigami
Shinigami2y ago
Yes If the logic for how you're multiplying is flawed then it'd obv show huge number
Despy()
Despy()OP2y ago
it is flawed thats what im asking for ; ; the logic itself
Angius
Angius2y ago
Well, show the code
Despy()
Despy()OP2y ago
sec
Shinigami
Shinigami2y ago
Show us what you've done, for us to understand where u went wrong
Despy()
Despy()OP2y 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);
Angius
Angius2y ago
Seems to work just fine
Angius
Angius2y ago
Despy()
Despy()OP2y ago
nevermind it was my mistake
Accord
Accord2y 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