Variables, types, and arithmetic · المتغيرات، الأنواع، والعمليات الحسابية
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
لكل قيمة نوع
- في C، لكل متغير نوع يحدد نوع القيمة التي يحملها.
- الأنواع الثلاثة الأكثر استخداماً:
int(عدد صحيح)،double(رقم عشري)، وchar(حرف واحد). - يجب تحديد النوع عند إنشاء المتغير:
int n = 5;أوdouble pi = 3.14;.
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
إجراء العمليات الحسابية
- C تقوم بالعمليات الحسابية باستخدام
+،-،*، و/، تماماً كالآلة الحاسبة. *و/يتم حسابهما قبل+و-. استخدم الأقواس( )لتغيير الترتيب.- عامل
%يعطي الباقي من القسمة:17 % 5هو2.
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
قسمة الأعداد الصحيحة بشكل مختلف
- عند قسمة رقمين
int، يتجاهل C الكسر:7 / 2هو3، وليس3.5. - للحفاظ على الكسر، يجب أن يكون أحد الطرفين على الأقل من نوع
double.7 / 2.0هو3.5. - يمكنك تحويل (cast)
intإلىdoubleباستخدام(double):(double)sum / count.
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
قراءة المدخلات
scanfيقرأ المدخلات المكتوبة ويخزنها في متغير.scanf("%d", &n);يقرأintواحداً ويضعها فيn.- لاحظ وجود
&قبلn:scanfتحتاج إلى العنوان (الموقع) للمتغير لكي تتمكن من ملئها. - في هذه الساحة التجريبية، يطلب Run (تشغيل) منك إدخال البيانات، قيمة واحدة في كل سطر؛ بينما يقوم Check answer (فحص الإجابة) بتغذية المثال المدخل لك تلقائياً.
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
أخطاء شائعة
- أعلن عن متغير مع نوعه:
int n;. - قسمة الأعداد الصحيحة تقطع الكسور:
7 / 2هي3، وليست3.5.
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
الآن جرب بنفسك
- استخدم
/و%للعمليات الحسابية للأعداد الصحيحة، واستخدمdouble(أو تحويل نوع) عندما تحتاج إلى كسر. - بالنسبة لمهام الدوال، لا تكتب
main— يوفره الفاحض لك. - اضغط تشغيل للمعالجة والتشغيل، ثم تحقق من الإجابة.
Variables hold a value · تحتوي المتغيرات على قيمة
A variable is a named box of a fixed type; assigning replaces it. · المتغير هو صندوق مسمى بنوع ثابت؛ الاستبدال يُغيّر قيمته.
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · اقرأ عددًا صحيحًا واحدًا n باستخدام scanf. يحتوي صندوق على 6 عناصر. اطبع سطرين بالضبط: Full boxes: ثم قيمة n / 6، وLeft over: ثم قيمة n % 6.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not write a main. · أكمل double average3(int a, int b, int c) لتُرجع المتوسط للأرقام الثلاثة كـ double. انتبه للقسمة الصحيحة — اقسم على 3.0، لا تقسم على 3. لا تكتب main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not write a main. · اكتب double hyp(double a, double b) تُرجع طول الوتر: الجذر التربيعي لـ a*a + b*b. استخدم sqrt من <math.h>. لا تكتب main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.