(C#) Can I multiply a string by an integer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

(C#) Can I multiply a string by an integer?

I was attempting the “Cheer Creator” challenge, which I had previously completed in Python, but when I tried to multiply the string “Ra!” by the input (which is an integer), it gives an error because the string cannot be multiplied by an integer, which I didn’t encounter as a problem in Python. I was wondering if there was a workaround to this, because I don’t want to have to type a scenario for each number between 1 and 10. Thanks for any help.

1st Apr 2023, 4:13 PM
Dfloomp
2 Answers
+ 6
It won't be possible just like Python, but you can do this: int mul = 4; Console.WriteLine( String.Concat( Enumerable.Repeat("Ra!", mul) ) ); But the task, I believe, is thought to be solved with loops. So, you do not need to type a scenario for each number 1 to 10, but only one loop and repeat as many times as you need the string printed using Console.Write, so that you don't have a line break in between.
1st Apr 2023, 5:10 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Oh ok. Thank you!
1st Apr 2023, 8:20 PM
Dfloomp