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

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

المطلوب

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


الحل بلغة 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; i++)
{
for (int j = 1; j <= n; j++)
{
if (i % 2 == 1 && j % 2 == 1 || i % 2 == 0 && j % 2 == 0)
{
printf("0 ");
}
else
{
printf("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; i++) { for (int j = 1; j <= n; j++) { if (i % 2 == 1 && j % 2 == 1 || i % 2 == 0 && j % 2 == 0) { printf("0 "); } else { printf("1 "); } } printf("\n"); } }

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

Enter the number of lines: 7
0 1 0 1 0 1 0 
1 0 1 0 1 0 1 
0 1 0 1 0 1 0 
1 0 1 0 1 0 1 
0 1 0 1 0 1 0 
1 0 1 0 1 0 1 
0 1 0 1 0 1 0