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.

Absolute.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; } 

תְפוּקָה

מבני נתונים ב-java
 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 loop

הבה נשקול דוגמה להדפסת הערך המוחלט בין שני מספרים שלמים באמצעות עבור לולאה ב-C תכנית.

Abs2.c

 #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() .

Abs.c

 #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) כדי להחזיר את המספר החיובי. ואם המספר חיובי, הוא מחזיר את אותו המספר.