Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي التاسع - حل التمرين الأول بلغة C#

المطلوب

أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أكبر من صفر حتى يتم رسم الشكل المطلوب بناءاً عليه.
ملاحظة: هنا افترضنا أن المستخدم قام بإدخال الرقم 3 عند التشغيل.


الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
int n;
do
{
Console.Write("Enter the number of lines: ");
n = int.Parse(Console.ReadLine());
}
while (n <= 0);
for (int i = 1; i <= n * 2; i++)
{
for (int j = 1; j <= n * 2; j++)
{
if (i <= n * 2 / 2)
{
Console.Write(i + " ");
}
else
{
Console.Write((n * 2 - i + 1) + " ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { int n; do { Console.Write("Enter the number of lines: "); n = int.Parse(Console.ReadLine()); } while (n <= 0); for (int i = 1; i <= n * 2; i++) { for (int j = 1; j <= n * 2; j++) { if (i <= n * 2 / 2) { Console.Write(i + " "); } else { Console.Write((n * 2 - i + 1) + " "); } } Console.WriteLine(); } Console.ReadKey(); } }

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 3 عند التشغيل.

Enter the number of lines: 3
1 1 1 1 1 1 
2 2 2 2 2 2 
3 3 3 3 3 3 
3 3 3 3 3 3 
2 2 2 2 2 2 
1 1 1 1 1 1