logo

Funkcia abs() v C

V tejto téme budeme diskutovať o funkcii abs v programovacom jazyku C. Funkcia abs () je preddefinovaná funkcia v hlavičkovom súbore stdlib.h na vrátenie absolútnej hodnoty daných celých čísel. Ak teda chceme vrátiť absolútnu hodnotu daného čísla, musíme implementovať stdlib.h hlavičkový súbor v programe C. Funkcia abs() vracia iba kladné čísla. Napríklad: Predpokladajme, že máme celé číslo -5 a chceme získať absolútne číslo, pomocou funkcie abs() vrátime kladné číslo ako 5. Okrem toho, ak prejdeme akékoľvek kladné číslo, vráti rovnaké číslo .

do reťazca
Funkcia abs() v C

Syntax

 int abs (int x); 

Vo vyššie uvedenej syntaxi je x celočíselný dátový typ, ktorý obsahuje záporné alebo kladné čísla a odovzdaný do funkcie abs() na vrátenie kladnej hodnoty, pretože funkcia má celočíselný dátový typ.

Poznámka: Funkcia abs() vždy vráti kladné číslo, aj keď je dané číslo záporné alebo kladné.

Program na získanie absolútnej hodnoty čísla pomocou funkcie abs().

Zoberme si príklad na vytlačenie absolútneho čísla pomocou funkcie abs() v programe C.

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

Výkon

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

Program na tlač absolútnych hodnôt daných celých čísel pomocou funkcie abs().

Vytvorme program na tlač absolútnych hodnôt daných čísel pomocou funkcie abs() v C.

Absolute.c

hashmap v jave
 #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; } 

Výkon

nohy vs noha
 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 

Program na tlač absolútnych hodnôt medzi dvoma celými číslami pomocou cyklu for

Zoberme si príklad na tlač absolútnej hodnoty medzi dvoma celými číslami pomocou pre slučku v C program.

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></=>

Program na získanie absolútnej hodnoty bez použitia funkcie abs().

Vytvorme program v C na získanie absolútnej hodnoty čísla bez použitia funkcie 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)>

Ako môžeme vidieť vo vyššie uvedenom programe, odovzdali sme celé číslo od používateľa. Ak je dané číslo záporné, vynásobí sa (-1), čím sa vráti kladné číslo. A ak je číslo kladné, vráti rovnaké číslo.