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

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

المطلوب

أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أكبر من صفر يمثل عدد أسطر الشكل الذي سيتم رسمه.


الحل بلغة 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; i++)
{
for (int j = 1; j <= n; j++)
{
if (j <= i)
{
Console.Write(i + " ");
}
}
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; i++) { for (int j = 1; j <= n; j++) { if (j <= i) { Console.Write(i + " "); } } Console.WriteLine(); } Console.ReadKey(); } }

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

Enter the number of lines: 6
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
		

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة