logo

abs() funkcija valodā C

Šajā tēmā mēs apspriedīsim abs funkciju C programmēšanas valodā. Funkcija abs () ir iepriekš definēta funkcija stdlib.h galvenes failā, lai atgrieztu doto veselo skaitļu absolūto vērtību. Tātad, ja mēs vēlamies atgriezt dotā skaitļa absolūto vērtību, mums ir jāievieš stdlib.h galvenes fails programmā C. Funkcija abs() atgriež tikai pozitīvos skaitļus. Piemēram: Pieņemsim, ka mums ir vesels skaitlis -5 un mēs vēlamies iegūt absolūto skaitli, mēs izmantojam funkciju abs(), lai atgrieztu pozitīvo skaitli kā 5. Turklāt, ja mēs nododam jebkuru pozitīvu skaitli, tas atgriež to pašu skaitli. .

abs() funkcija C

Sintakse

 int abs (int x); 

Iepriekš minētajā sintaksē x ir vesela skaitļa datu tips, kurā ir negatīvi vai pozitīvi skaitļi un kas tiek nodots funkcijā abs(), lai atgrieztu pozitīvo vērtību, jo funkcijai ir vesela skaitļa datu tips.

int virknē

Piezīme. Funkcija abs() vienmēr atgriež pozitīvu skaitli, pat ja norādītais skaitlis ir negatīvs vai pozitīvs.

Programma, lai iegūtu skaitļa absolūto vērtību, izmantojot funkciju abs().

Apskatīsim piemēru, kā izdrukāt absolūto skaitli, izmantojot funkciju abs() programmā 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; } 

Izvade

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

Programma, lai izdrukātu doto veselo skaitļu absolūtās vērtības, izmantojot funkciju abs().

Izveidosim programmu, kas drukātu doto skaitļu absolūtās vērtības, izmantojot abs() funkciju C valodā.

Absolūti.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; } 

Izvade

 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 

Programma, lai drukātu absolūtās vērtības starp diviem veseliem skaitļiem, izmantojot cilpu

Apskatīsim piemēru, kā izdrukāt absolūto vērtību starp diviem veseliem skaitļiem, izmantojot cilpai C programma.

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

Programma, lai iegūtu absolūto vērtību, neizmantojot funkciju abs().

Izveidosim programmu C, lai iegūtu skaitļa absolūto vērtību, neizmantojot funkciju abs().

Abs.c

kā pārvērst virkni int java
 #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)>

Kā redzams iepriekš minētajā programmā, no lietotāja esam nosūtījuši veselu skaitli. Ja dotais skaitlis ir negatīvs, tas tiks reizināts ar (-1), lai atgrieztu pozitīvo skaitli. Un, ja skaitlis ir pozitīvs, tas atgriež to pašu skaitli.