تحديات برمجيةالتحدي العاشر - حل التمرين الثالث بلغة C
المطلوب
أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد بين 1 و 9 يمثل عدد أسطر الشكل الذي سيتم رسمه.
الحل بلغة C
#include <stdio.h>
void main() {
int n;
do
{
printf("Enter the number of lines: ");
scanf("%d", &n);
}
while (n < 1 || n > 9);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j == n - i + 1)
{
printf("1 ");
}
else if (j == n)
{
printf("%d ", i);
}
else if (i == n)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
#include <stdio.h>
void main() {
int n;
do
{
printf("Enter the number of lines: ");
scanf("%d", &n);
}
while (n < 1 || n > 9);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j == n - i + 1)
{
printf("1 ");
}
else if (j == n)
{
printf("%d ", i);
}
else if (i == n)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 6 عند التشغيل.
Enter the number of lines: 6 1 1 2 1 3 1 4 1 5 1 2 3 4 5 6