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

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

المطلوب

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


الحل بلغة C

#include <stdio.h>
void main()
{
int n;
do
{
printf("Enter the number of lines: ");
scanf("%d", &n);
}
while (n <= 0);
for (int i = 1; i <= n * 2; i++)
{
for (int j = 1; j <= n * 2; j++)
{
if (i <= n * 2 / 2)
{
printf("%d ", i);
}
else
{
printf("%d ", (n * 2 - i + 1));
}
}
printf("\n");
}
}
#include <stdio.h> void main() { int n; do { printf("Enter the number of lines: "); scanf("%d", &n); } while (n <= 0); for (int i = 1; i <= n * 2; i++) { for (int j = 1; j <= n * 2; j++) { if (i <= n * 2 / 2) { printf("%d ", i); } else { printf("%d ", (n * 2 - i + 1)); } } printf("\n"); } }

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 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