C# program to print stars in diamond shape




















In this post I am explaining that how to print a diamond pattern type output using C. The diamond pattern question is often asked in interviews. There can be many ways of thinking to print diamond type output.

Here I am explaining the easiest one in which I used nested for loop. Below is the source code to print a diamond using nested loop. The program is successfully compiled and executed with Microsoft Visual Studio. Here I used 2 nested for loop , one to print the simple pyramid with x rows and another to print the reverse pyramid pattern with x-1 rows. Now you have printed the numbers Diamond type output and looking to print the stars diamond pattern. Printing stars diamond pattern is very easy.

Simply copy the above numbers diamond program and replace console. WriteLine Number ; with Console. I am true. Add a comment. Active Oldest Votes. Improve this answer. Dmitry Dmitry I took a look inside the constructor.

You get bonuses with this because a you only need to allocate the memory for that string once and b the loop uses pointers to break the computation into blocks of 4. See CtorCharCount in System. String — Matthew Frontino. Except the title of the question specifically states that the point of the exercise is to use for loops.

This is clearly someone learning to code, not someone looking for the most efficient way to print asterisks to the console! If you want to take an input from your user, try this: int number; int i, j, k; if int. TryParse Console. WriteLine "No number entered. Read ; It is possible and better to do this with just one loop, but your approach also works, and as for loops are probably the point of the exercise your way is effective. First line: no spaces, 5 stars.

Second line: 1 space each side, 3 stars. Third line: 2 spaces each side, 1 star. Hope this helps you think about how to model the problem at hand! Thanks for this guys. I ran the code and it seemed to produce the result you are looking for, does it work for you? What I really want to do is when a user enters a value it print that number of lines.

OK, I will update my answer to include the option of entering a number of lines. If it helps, please mark it as answer! ToDouble 4. Maksim Simkin 9, 4 4 gold badges 37 37 silver badges 48 48 bronze badges. ReadLine ; Output Image:. Rizier 57k 16 16 gold badges 88 88 silver badges bronze badges. Ewan Ewan 1, 1 1 gold badge 11 11 silver badges 24 24 bronze badges. A better answer would be if you found a way to do it with 1 loop — Matthew Frontino. Hah, great enthusiasm!

Unfortunately, I think the best answer eliminated the extra loops. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science.



0コメント

  • 1000 / 1000