logo

وظيفة abs () في C

في هذا الموضوع سنتحدث عن وظيفة abs في لغة البرمجة C. الدالة abs () هي دالة محددة مسبقًا في ملف الرأس stdlib.h لإرجاع القيمة المطلقة للأعداد الصحيحة المحددة. لذا، إذا أردنا إرجاع القيمة المطلقة لرقم معين، فنحن بحاجة إلى تنفيذ الأمر stdlib.h ملف الرأس في برنامج C. تقوم الدالة abs() بإرجاع الأرقام الموجبة فقط. على سبيل المثال: لنفترض أن لدينا عددًا صحيحًا -5، ونريد الحصول على الرقم المطلق، نستخدم الدالة abs() لإرجاع الرقم الموجب على هيئة 5. علاوة على ذلك، إذا مررنا أي رقم موجب، فإنه يعيد نفس الرقم .

وظيفة abs () في C

بناء الجملة

 int abs (int x); 

في بناء الجملة أعلاه، x هو نوع بيانات عدد صحيح يحتوي على أرقام سالبة أو موجبة ويتم تمريره في الدالة abs() لإرجاع القيمة الموجبة لأن الدالة تحتوي على نوع بيانات عدد صحيح.

ملاحظة: تقوم الدالة abs() دائمًا بإرجاع رقم موجب حتى لو كان الرقم المحدد سالبًا أو موجبًا.

برنامج للحصول على القيمة المطلقة لرقم باستخدام الدالة abs()

لنأخذ مثالاً لطباعة العدد المطلق باستخدام الدالة abs() في برنامج C.

معجميا

بروغ.ج

 #include #include // use stdlib.h header file to use abs() function. int main() { int num, n; // declare the local variable printf (' Enter a number to display the absolute value: '); scanf ('%d', &num); /* define the abs() function to convert the given number into the absolute value. */ n = abs (num); printf ('
 The absolute value of %d is %d. ', num, n); return 0; } 

انتاج |

 Enter a number to display the absolute value: -35 The absolute value of -35 is 35. 

برنامج لطباعة القيم المطلقة للأعداد الصحيحة المعطاة باستخدام الدالة abs()

لنقم بإنشاء برنامج لطباعة القيم المطلقة للأرقام المحددة باستخدام الدالة abs() في لغة C.

مطلق.ج

جافا إذا البيان
 #include #include // use stdlib.h header file to use abs() function. #include int main() { printf (' The absolute value of 27 is %d ', abs (27)); printf (' 
 The absolute value of -16 is %d ', abs (-16)); printf (' 
 The absolute value of -125 is %d ', abs (-125)); printf (' 
 The absolute value of 18 is %d ', abs (18)); printf (' 
 The absolute value of -29 is %d ', abs (-29)); printf (' 
 The absolute value of 0 is %d ', abs (0)); return 0; } 

انتاج |

 The absolute value of 27 is 27 The absolute value of -16 is 16 The absolute value of -125 is 125 The absolute value of 18 is 18 The absolute value of -29 is 29 The absolute value of 0 is 0 

برنامج لطباعة القيم المطلقة بين عددين صحيحين باستخدام حلقة for

لنفكر في مثال لطباعة القيمة المطلقة بين عددين صحيحين باستخدام للحلقة في C برنامج.

عبس2.ج

 #include #include #include int main() { int i, num, last; printf (' Enter the first number: 
 '); scanf (' %d', &num); printf ('
 Enter the last number from which you want to get the absolute number: &apos;); scanf (&apos; %d&apos;, &amp;last); // use for loop to print the absolute number for (i = num; i <= last; i++) { abs() function convert a negative number to positive printf( '
 the absolute value of %d is %d. ', i, abs( i)); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the first negative number: -5 Enter the last number from which you want to get the absolute number: 5 The absolute value of -5 is 5. The absolute value of -4 is 4. The absolute value of -3 is 3. The absolute value of -2 is 2. The absolute value of -1 is 1. The absolute value of 0 is 0. The absolute value of 1 is 1. The absolute value of 2 is 2. The absolute value of 3 is 3. The absolute value of 4 is 4. The absolute value of 5 is 5. </pre> <h3>Program to get the absolute value without using the abs() function</h3> <p>Let&apos;s create a C program to get the absolute value of a number without using the abs() function.</p> <p> <strong>Abs.c</strong> </p> <pre> #include #include // use stdlib.h header file to use abs() function. int getAbsolute (int num) { /* if the passed value (num) is less than 0 (zero), the number multiplied by (-1) to return an absolute value. */ if (num <0) { num="(" -1 ) * num; given negative number multiplied by (-1) printf (' the absolute value is: %d', num); } else return int main() enter a to display value: '); scanf ('%d', &num); call functon getabsolute(num); 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to display the absolute value: -8 The absolute value is: 8 </pre> <p>As we can see in the above program, we have passed an integer number from the user. If the given number is negative, it will be multiplied by (-1) to return the positive number. And if the number is positive, it returns the same number.</p> <hr></0)></pre></=>

برنامج للحصول على القيمة المطلقة دون استخدام الدالة abs()

لنقم بإنشاء برنامج بلغة C للحصول على القيمة المطلقة لرقم دون استخدام الدالة abs().

أبس.ج

 #include #include // use stdlib.h header file to use abs() function. int getAbsolute (int num) { /* if the passed value (num) is less than 0 (zero), the number multiplied by (-1) to return an absolute value. */ if (num <0) { num="(" -1 ) * num; given negative number multiplied by (-1) printf (\' the absolute value is: %d\', num); } else return int main() enter a to display value: \'); scanf (\'%d\', &num); call functon getabsolute(num); 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to display the absolute value: -8 The absolute value is: 8 </pre> <p>As we can see in the above program, we have passed an integer number from the user. If the given number is negative, it will be multiplied by (-1) to return the positive number. And if the number is positive, it returns the same number.</p> <hr></0)>

كما نرى في البرنامج أعلاه، لقد مررنا رقمًا صحيحًا من المستخدم. إذا كان الرقم المحدد سالبًا، فسيتم ضربه في (-1) لإرجاع الرقم الموجب. وإذا كان الرقم موجبا، فإنه يعيد نفس الرقم.