التمرين الثاني
أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أسطر الشكل الذي سيتم رسمه.
إنتبه: يجب أن يدخل المستخدم عدد مفرد و أكبر من صفر, لأن عدد الأسطر لا يمكن أن يكون مزدوجاً أو قيمته تساوي صفر أو أقل من صفر.
* * * * * * * * * * * * * * * *
الحل بلغة بايثون
الطريقة الأولى لحل التمرين.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n + 1):
if i <= n / 2:
print(' ' * (int(n / 2) - i + 1), end='')
print('*', end='')
print(' ' * ((i * 2) - 3), end='')
if i != 1:
print('*', end='')
else:
print(' ' * (i - int(n / 2) - 1), end='')
print('*', end='')
print(' ' * ((n * 2) - (i * 2) - 1), end='')
if i != n:
print('*', end='')
print()
الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n + 1):
if i <= n / 2:
for k in range(1, int(n / 2) - i + 2):
print(' ', end='')
print('*', end='')
for k in range(1, (i * 2) - 2):
print(' ', end='')
if i != 1:
print('*', end='')
else:
for k in range(1, i - int(n / 2)):
print(' ', end='')
print('*', end='')
for k in range(1, (n * 2) - (i * 2)):
print(' ', end='')
if i != n:
print('*', end='')
print()
الطريقة الثالثة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
i = 1
while i <= n:
if i <= n / 2:
k = 1
while k <= int(n / 2) - i + 1:
print(' ', end='')
k += 1
print('*', end='')
k = 1
while k <= (i * 2) - 3:
print(' ', end='')
k += 1
if i != 1:
print('*', end='')
else:
k = 1
while k < i - int(n / 2):
print(' ', end='')
k += 1
print('*', end='')
k = 1
while k < (n * 2) - (i * 2):
print(' ', end='')
k += 1
if i != n:
print('*', end='')
print()
i += 1
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 9 عند التشغيل.
Enter the number of lines: 9
*
* *
* *
* *
* *
* *
* *
* *
*
الحل بلغة جافا
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
do {
System.out.print("Enter the number of lines: ");
n = input.nextInt();
}
while( n<=0 || n%2==0 );
for (int i=1; i<=n; i++)
{
if(i<=n/2)
{
for (int k=1; k<(n/2)-i+2; k++)
{
System.out.print(" ");
}
System.out.print("*");
for (int k=1; k<(i*2)-2; k++)
{
System.out.print(" ");
}
if(i != 1) {
System.out.print("*");
}
}
else
{
for (int k=1; k<i-(n/2); k++)
{
System.out.print(" ");
}
System.out.print("*");
for (int k=1; k<(n*2)-(i*2); k++)
{
System.out.print(" ");
}
if(i != n) {
System.out.print("*");
}
}
System.out.println();
}
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 9 عند التشغيل.
Enter the number of lines: 9 * * * * * * * * * * * * * * * *
الحل بلغة C
#include<stdio.h>
void main() {
int n;
do {
printf("Enter the number of lines: ");
scanf("%d", &n);
}
while( n<=0 || n%2==0 );
for( int i=1; i<=n; i++ )
{
if( i<=n/2 )
{
for( int k=1; k<(n/2)-i+2; k++ )
{
printf(" ");
}
printf("*");
for ( int k=1; k<(i*2)-2; k++ )
{
printf(" ");
}
if( i!=1 )
{
printf("*");
}
}
else
{
for( int k=1; k<i-(n/2); k++ )
{
printf(" ");
}
printf("*");
for ( int k=1; k<(n*2)-(i*2); k++ )
{
printf(" ");
}
if( i!=n )
{
printf("*");
}
}
printf("\n");
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 9 عند التشغيل.
Enter the number of lines: 9 * * * * * * * * * * * * * * * *
الحل بلغة 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++)
{
if (i <= n / 2)
{
for (int k = 1; k < (n / 2) - i + 2; k++)
{
Console.Write(" ");
}
Console.Write("*");
for (int j = 1; j < (i * 2) - 2; j++)
{
Console.Write(" ");
}
if(i != 1)
{
Console.Write("*");
}
}
else
{
for (int k = 1; k < i - (n / 2); k++)
{
Console.Write(" ");
}
Console.Write("*");
for (int j = 1; j < (n * 2) - (i * 2); j++)
{
Console.Write(" ");
}
if (i != n)
{
Console.Write("*");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 9 عند التشغيل.
Enter the number of lines: 9 * * * * * * * * * * * * * * * *
الحل بلغة C++
#include <iostream>
int main() {
int n;
do {
std::cout << "Enter the number of lines: ";
std::cin >> n;
}
while (n <= 0);
for (int i = 1; i <= n; i++)
{
if (i <= n / 2)
{
for (int k = 1; k < (n / 2) - i + 2; k++)
{
std::cout << " ";
}
std::cout << "*";
for (int j = 1; j < (i * 2) - 2; j++)
{
std::cout << " ";
}
if (i != 1)
{
std::cout << "*";
}
}
else
{
for (int k = 1; k < i - (n / 2); k++)
{
std::cout << " ";
}
std::cout << "*";
for (int j = 1; j < (n * 2) - (i * 2); j++)
{
std::cout << " ";
}
if (i != n)
{
std::cout << "*";
}
}
std::cout << "\n";
}
char end; std::cin >> end;
return 0;
}
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 9 عند التشغيل.
Enter the number of lines: 9 * * * * * * * * * * * * * * * *