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

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

المطلوب

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

  • الخيار 0 لإيقاف البرنامج.
  • الخيار 1 لطباعة ناتج جمع العددين.
  • الخيار 2 لطباعة ناتج طرح العددين.
  • الخيار 3 لطباعة ناتج ضرب العددين.
  • الخيار 4 لطباعة ناتج قسمة العددين.

الحل بلغة C

#include <stdio.h>
void main() {
float a, b;
int option = -1;
printf("Enter first number: ");
scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
printf(
"\nWrite down the operation number then press enter\n"
"0: To end the program.\n"
"1: To print the sum.\n"
"2: To print the subtraction.\n"
"3: To print the multiplication.\n"
"4: To print the division.");
while (option != 0)
{
printf("\n-------------------------");
printf("\nEnter option: ");
scanf("%d", &option);
switch (option)
{
case 0:
printf("Program end.");
break;
case 1:
printf("%f + %f = %f", a, b, a + b);
break;
case 2:
printf("%f - %f = %f", a, b, a - b);
break;
case 3:
printf("%f * %f = %f", a, b, a * b);
break;
case 4:
printf("%f / %f = %f", a, b, a / b);
break;
}
}
}
#include <stdio.h> void main() { float a, b; int option = -1; printf("Enter first number: "); scanf("%f", &a); printf("Enter second number: "); scanf("%f", &b); printf( "\nWrite down the operation number then press enter\n" "0: To end the program.\n" "1: To print the sum.\n" "2: To print the subtraction.\n" "3: To print the multiplication.\n" "4: To print the division."); while (option != 0) { printf("\n-------------------------"); printf("\nEnter option: "); scanf("%d", &option); switch (option) { case 0: printf("Program end."); break; case 1: printf("%f + %f = %f", a, b, a + b); break; case 2: printf("%f - %f = %f", a, b, a - b); break; case 3: printf("%f * %f = %f", a, b, a * b); break; case 4: printf("%f / %f = %f", a, b, a / b); break; } } }

إذا قام المستخدم بإدخال الرقمين 1 و 4. ثم قام بتجربة كل الخيارات الموجودة في القائمة, فستكون النتيجة كالتالي.
قمنا بتعليم المعلومات التي أدخلها المستخدم باللون الأصفر و النتائج التي ظهرت له بعد إدخالها باللون الأزرق.

Enter first number:  1 
Enter second number:  4 

Write down the operation number then press enter
0: To end the program.
1: To print the sum.
2: To print the subtraction.
3: To print the multiplication.
4: To print the division.
-------------------------
Enter option:  1 
1 + 4 = 5
-------------------------
Enter option:  2 
1 - 4 = -3
-------------------------
Enter option:  3 
1 * 4 = 4
-------------------------
Enter option:  4 
1 / 4 = 0.25
-------------------------
Enter option:  0 
Program end.