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 <iostream>
int main() {
int a, b, c, maximum;
std::cout << "Enter a: ";
std::cin >> a;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter c: ";
std::cin >> c;
maximum = (a > b) ? a : b;
maximum = (maximum > c) ? maximum : c;
std::cout << "The max number is: " << maximum;
char end; std::cin >> end;
return 0;
}
#include <iostream> int main() { int a, b, c, maximum; std::cout << "Enter a: "; std::cin >> a; std::cout << "Enter b: "; std::cin >> b; std::cout << "Enter c: "; std::cin >> c; maximum = (a > b) ? a : b; maximum = (maximum > c) ? maximum : c; std::cout << "The max number is: " << maximum; char end; std::cin >> end; return 0; }

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

#include <iostream>
int main() {
int a, b, c, maximum;
std::cout << "Enter a: ";
std::cin >> a;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter c: ";
std::cin >> c;
if (a > b && a > c)
{
maximum = a;
}
else if (b > a && b > c)
{
maximum = b;
}
else
{
maximum = c;
}
std::cout << "The max number is: " << maximum;
char end; std::cin >> end;
return 0;
}
#include <iostream> int main() { int a, b, c, maximum; std::cout << "Enter a: "; std::cin >> a; std::cout << "Enter b: "; std::cin >> b; std::cout << "Enter c: "; std::cin >> c; if (a > b && a > c) { maximum = a; } else if (b > a && b > c) { maximum = b; } else { maximum = c; } std::cout << "The max number is: " << maximum; char end; std::cin >> end; return 0; }

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

#include <iostream>
int main() {
int a, b, c, maximum;
std::cout << "Enter a: ";
std::cin >> a;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter c: ";
std::cin >> c;
if (a > b && a > c)
{
maximum = a;
}
else
{
maximum = b;
}
if (maximum < c)
{
maximum = c;
}
std::cout << "The max number is: " << maximum;
char end; std::cin >> end;
return 0;
}
#include <iostream> int main() { int a, b, c, maximum; std::cout << "Enter a: "; std::cin >> a; std::cout << "Enter b: "; std::cin >> b; std::cout << "Enter c: "; std::cin >> c; if (a > b && a > c) { maximum = a; } else { maximum = b; } if (maximum < c) { maximum = c; } std::cout << "The max number is: " << maximum; char end; std::cin >> end; return 0; }

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

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