التمرين الخامس
أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أسطر و أعمدة الشكل الذي سيتم رسمه.
إنتبه: يجب أن يدخل المستخدم عددين أكبر من صفر, لأن عدد أسطر و أعمدة الرسمة لا يمكن أن يكون صفر أو أقل من صفر.
***** * * * * * * * * * * * * * * * * *****
الحل بلغة بايثون
الطريقة الأولى لحل التمرين.
n = 0
while n <= 0:
n = int(input("Enter the height: "))
m = 0
while m <= 0:
m = int(input("Enter the width: "))
for i in range(1, n+1):
if i == 1 or i == n:
print('*' * m)
else:
print("*", end='')
print(' ' * (m - 2), end='')
print("*")
الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0:
n = int(input("Enter the height: "))
m = 0
while m <= 0:
m = int(input("Enter the width: "))
for i in range(1, n+1):
if i == 1 or i == n:
for j in range(1, m+1):
print('*', end='')
print()
else:
print("*", end='')
for k in range(1, m-1):
print(' ', end='')
print("*")
الطريقة الثالثة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0:
n = int(input("Enter the height: "))
m = 0
while m <= 0:
m = int(input("Enter the width: "))
i = 1
while i <= n:
if i == 1 or i == n:
j = 1
while j <= m:
print('*', end='')
j += 1
print()
else:
print("*", end='')
k = 1
while k < m - 1:
print(' ', end='')
k += 1
print("*")
i += 1
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 كطول المستطيل و الرقم 10 كعرض المستطيل عند التشغيل.
Enter the height: 5 Enter the width: 10 ********** * * * * * * **********
و سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 10 كطول المستطيل و الرقم 5 كعرض المستطيل عند التشغيل.
Enter the height: 10 Enter the width: 5 ***** * * * * * * * * * * * * * * * * *****
الحل بلغة جافا
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
int m;
do {
System.out.print("Enter the height: ");
n = input.nextInt();
}
while (n <= 0);
do {
System.out.print("Enter the width: ");
m = input.nextInt();
}
while (m <= 0);
for (int i = 1; i <= n; i++)
{
if (i == 1 || i == n)
{
for (int j = 1; j <= m; j++)
{
System.out.print("*");
}
}
else
{
System.out.print("*");
for (int k = 1; k < m - 1; k++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 كطول المستطيل و الرقم 10 كعرض المستطيل عند التشغيل.
Enter the height: 5 Enter the width: 10 ********** * * * * * * **********
و سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 10 كطول المستطيل و الرقم 5 كعرض المستطيل عند التشغيل.
Enter the height: 10 Enter the width: 5 ***** * * * * * * * * * * * * * * * * *****
الحل بلغة C++
#include <iostream>
int main() {
int n;
int m;
do {
std::cout << "Enter the height: ";
std::cin >> n;
}
while (n <= 0);
do {
std::cout << "Enter the width: ";
std::cin >> m;
}
while (m <= 0);
for (int i = 1; i <= n; i++)
{
if (i == 1 || i == n)
{
for (int j = 1; j <= m; j++)
{
std::cout << "*";
}
}
else
{
std::cout << "*";
for (int k = 1; k < m - 1; k++)
{
std::cout << " ";
}
std::cout << "*";
}
std::cout << "\n";
}
char end; std::cin >> end;
return 0;
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 كطول المستطيل و الرقم 10 كعرض المستطيل عند التشغيل.
Enter the height: 5 Enter the width: 10 ********** * * * * * * **********
و سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 10 كطول المستطيل و الرقم 5 كعرض المستطيل عند التشغيل.
Enter the height: 10 Enter the width: 5 ***** * * * * * * * * * * * * * * * * *****
الحل بلغة C#
using System;
class Program
{
static void Main(string[] args)
{
int n;
int m;
do
{
Console.Write("Enter the height: ");
n = int.Parse(Console.ReadLine());
}
while (n <= 0);
do
{
Console.Write("Enter the width: ");
m = int.Parse(Console.ReadLine());
}
while (m <= 0);
for (int i = 1; i <= n; i++)
{
if (i == 1 || i == n)
{
for (int j = 1; j <= m; j++)
{
Console.Write("*");
}
}
else
{
Console.Write("*");
for (int k = 1; k < m - 1; k++)
{
Console.Write(" ");
}
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 كطول المستطيل و الرقم 10 كعرض المستطيل عند التشغيل.
Enter the height: 5 Enter the width: 10 ********** * * * * * * **********
و سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 10 كطول المستطيل و الرقم 5 كعرض المستطيل عند التشغيل.
Enter the height: 10 Enter the width: 5 ***** * * * * * * * * * * * * * * * * *****
الحل بلغة C++
#include <iostream>
int main() {
int n;
int m;
do {
std::cout << "Enter the height: ";
std::cin >> n;
}
while (n <= 0);
do {
std::cout << "Enter the width: ";
std::cin >> m;
}
while (m <= 0);
for (int i = 1; i <= n; i++)
{
if (i == 1 || i == n)
{
for (int j = 1; j <= m; j++)
{
std::cout << "*";
}
}
else
{
std::cout << "*";
for (int k = 1; k < m - 1; k++)
{
std::cout << " ";
}
std::cout << "*";
}
std::cout << "\n";
}
char end; std::cin >> end;
return 0;
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 كطول المستطيل و الرقم 10 كعرض المستطيل عند التشغيل.
Enter the height: 5 Enter the width: 10 ********** * * * * * * **********
و سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 10 كطول المستطيل و الرقم 5 كعرض المستطيل عند التشغيل.
Enter the height: 10 Enter the width: 5 ***** * * * * * * * * * * * * * * * * *****