How do I multiply string? | SoloLearn: Learn to code for FREE!

+5

# How do I multiply string?

I have a string of '5,6' as user input, How do i multply to get the answer 5*6= 30

7/3/2020 12:46:49 PM

Ashraf

+12

Ashraf You can do like this string str = Console.ReadLine(); string[] arr = str.Split(","); int mult = 1; foreach(string s in arr) { mult = mult * Convert.ToInt32(s); } Console.WriteLine(mult);

+6

Ashraf Convert the String into integer array and multiply the values.

+6

Thanks AJ Anant - C#/Java Challenger bro and alll others

+4

Use long multiication as you used to do in school i.e start from rightmost chars in both strings, multiply each digit while taking care of carry.

+4

I am not a c# expert, meaning that it could exist a library for that purpose, but a simple technique is transforming the string into a string or character array, then convert each character (or string) into integer and multiply them.

+4

if your input is a string, you have to parse it to get the integers. just google c# parse string

+4

12 * 15 = 180 carry = 0 12 15 ________ 5 *2 + carry = 10,, you write 0 and c = 1 5 * 1 + carry = 6, reset carry, c = 0 your string currently is 60, now you do 12 * 1 12 x 1 ____ 2, add 6 (start pos in upper string) + carry 2 + 6 + 0 = 8 carry = 0, write 8 at the position of 6, intermediate result 80 then multiply 1 * 1 =1, write 1 at left of 8 in temp string, so you have 180 123 * 45 ------ 615 4920 ------ 5535

+2

~ swim ~ can you please show me an example!

0

By casting