How do I print an n-line diamond in C #? Practical explanation

Source: Internet
Author: User

The idea of solving problems: First we print a diamond of n rows, the general diamond is odd line (look at a little), so we print out is an odd line of diamond, generally this form:

problem-solving ideas: We can think of the diamond as two parts, as the above a positive triangle and the following an anti-triangular composition, and then we can print out separately.

 Console.WriteLine ("Enter a positive integer greater than 2");//At least 3 rows to make a diamond, enter an even-numbered diamond is this even minus 1 lines int n = C Onvert.            ToInt32 (Console.ReadLine ()); for (int i = 1; I <= (n + 1)/2; i++)//print the upper portion of the diamond {for (int j = (n-1)/2; J >= i; j--                ) {//Print space Console.Write (""); } for (int k = 1; k <= i * 2-1; k++) {//print "*" number, line I have i*2-1 "*" Con Sole.                Write ("*");            } Console.WriteLine (); } for (int i = (n-1)/2; I >= 1; i--)//print Diamond The following section is the same as the above section {for (int j = i-1; J < (n-1)/2;                J + +) {Console.Write ("");                } for (int k = 1; k <= i * 2-1; k++) {Console.Write ("*");            } Console.WriteLine (); }

If there is something wrong with the

, please advise us and welcome your questions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: