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

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

المطلوب

أكتب برنامج يطلب من المستخدم إدخال ثلاث أرقام و خزنها في ثلاث متغيرات (a - b - c ), ثم يعرض له أكبر رقم تم إدخاله.
مثال: إذا قام المستخدم بإدخال الأرقام 2, 7 و 5 فستكون النتيجة كالتالي.

Enter a: 1
Enter b: 2
Enter c: 5
The max number is: 5
	

الحل بلغة C

الطريقة الأولى لحل التمرين.

#include <stdio.h>
void main() {
int a, b, c, maximum;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
printf("Enter c: ");
scanf("%d", &c);
maximum = (a > b)? a: b;
maximum = (maximum > c)? maximum: c;
printf("The max number is: %d", maximum);
}
#include <stdio.h> void main() { int a, b, c, maximum; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); printf("Enter c: "); scanf("%d", &c); maximum = (a > b)? a: b; maximum = (maximum > c)? maximum: c; printf("The max number is: %d", maximum); }

الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.

#include <stdio.h>
void main() {
int a, b, c, maximum;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
printf("Enter c: ");
scanf("%d", &c);
if (a > b && a > c)
{
maximum = a;
}
else if (b > a && b > c)
{
maximum = b;
}
else
{
maximum = c;
}
printf("The max number is: %d", maximum);
}
#include <stdio.h> void main() { int a, b, c, maximum; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); printf("Enter c: "); scanf("%d", &c); if (a > b && a > c) { maximum = a; } else if (b > a && b > c) { maximum = b; } else { maximum = c; } printf("The max number is: %d", maximum); }

الطريقة الثالثة لحل التمرين و الحصول على نفس النتيجة.

#include <stdio.h>
void main() {
int a, b, c, maximum;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
printf("Enter c: ");
scanf("%d", &c);
if (a > b && a > c)
{
maximum = a;
}
else
{
maximum = b;
}
if (maximum < c)
{
maximum = c;
}
printf("The max number is: %d", maximum);
}
#include <stdio.h> void main() { int a, b, c, maximum; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); printf("Enter c: "); scanf("%d", &c); if (a > b && a > c) { maximum = a; } else { maximum = b; } if (maximum < c) { maximum = c; } printf("The max number is: %d", maximum); }

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

Enter a: 1
Enter b: 2
Enter c: 5
The max number is: 5