-2

I need to have a user enter a number between 1 - 80 and have it print however many * the number is I have managed to get it to print the correct number of * just not on one line.

  • 1
    Show your code, please. And normally you would use a loop. – ProgrammingLlama Jun 21 '21 at 03:25
  • 1
    `string oneLine = new string('*', number);`? – 41686d6564 stands w. Palestine Jun 21 '21 at 03:31
  • Well I just managed to get it on one line I was using Console.Writeline where Console.Write fixed it. Now I just need to get the loop to circle back. while (1 <= num & num <= 80) // if the number is between 1 & 80 it passses { while (counter != num) { Console.Write("*",num); //outputs the number of * counter++ // Output your line of * per the directions } – Johnathon Jun 21 '21 at 03:36
  • You should ask a new question, really. SO won't run out of question IDs any time soon! :) Not really sure what circle back means - you want to return the cursor to the start of the line for when you next print? Use a single call to `Console.WriteLine()`. You want counter to be 1 again? Just set it to 1 after the loop – Caius Jard Jun 21 '21 at 03:52
  • Does this answer your question [Can I “multiply” a string (in C#)?](https://stackoverflow.com/questions/532892/can-i-multiply-a-string-in-c) and [How to print the same character many times with Console.WriteLine()](https://stackoverflow.com/questions/13456665/how-to-print-the-same-character-many-times-with-console-writeline) –  Jun 21 '21 at 06:18

3 Answers3

2

I have managed to get it to print the correct number of * just not on one line.

I suspect you've used Console.WriteLine

Use Console.Write instead


Always (always) post the code you've written, that is malfunctioning, so we can see it

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

here is an exemple of printing ***** in one line. In this exemple, I stop the loop when i is not <5 but you can replace 5 with a number you want.

for (int i=0; i<5; i++)
{
    Console.Write("*");
}
stic-lab
  • 433
  • 7
  • 16
-1

For an easy one liner

var starString = string.Concat(Enumerable.Repeat("*", number));

alternatively if you need a loop for whatever reason

string starString = "";
for(var i = 0; i< number;i++)
     starString += "*";
The Lemon
  • 1,211
  • 15
  • 26
  • 1
    100 string concatenations should really use a string builder.. also see Ahmed's comment about using the string constructor designed for this – Caius Jard Jun 21 '21 at 03:35