logo

C'de abs () işlevi

Bu konu başlığımızda C programlama dilinde abs fonksiyonunu ele alacağız. Abs () işlevi, verilen tam sayıların mutlak değerini döndürmek için stdlib.h başlık dosyasında önceden tanımlanmış bir işlevdir. Dolayısıyla, belirli bir sayının mutlak değerini döndürmek istiyorsak, aşağıdakileri uygulamamız gerekir: stdlib.h C programında başlık dosyası. abs() işlevi yalnızca pozitif sayıları döndürür. Örneğin: Elimizde -5 olan bir tam sayı olduğunu ve mutlak sayıyı elde etmek istediğimizi varsayalım, pozitif sayıyı 5 olarak döndürmek için abs() fonksiyonunu kullanıyoruz. Ayrıca herhangi bir pozitif sayıyı iletirsek aynı sayıyı döndürür. .

C'de abs () işlevi

Sözdizimi

 int abs (int x); 

Yukarıdaki sözdiziminde x, negatif veya pozitif sayıları tutan bir tamsayı veri türüdür ve fonksiyon bir tamsayı veri tipine sahip olduğundan pozitif değeri döndürmek için abs() fonksiyonuna geçirilir.

Not: abs() işlevi, verilen sayı negatif veya pozitif olsa bile her zaman pozitif bir sayı döndürür.

abs() fonksiyonunu kullanarak bir sayının mutlak değerini elde eden program

C programında abs() fonksiyonunu kullanarak mutlak sayıyı yazdırmak için bir örnek düşünelim.

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

Çıktı

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

abs() fonksiyonunu kullanarak verilen tam sayıların mutlak değerlerini yazdıran program

C dilinde abs() fonksiyonunu kullanarak verilen sayıların mutlak değerlerini yazdıran bir program oluşturalım.

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

Çıktı

 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 döngüsünü kullanarak iki tam sayı arasındaki mutlak değerleri yazdıran program

İki tam sayı arasındaki mutlak değeri kullanarak yazdırmak için bir örnek düşünelim. C'deki for döngüsü 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></=>

abs() fonksiyonunu kullanmadan mutlak değeri elde eden program

Abs() fonksiyonunu kullanmadan bir sayının mutlak değerini elde etmek için bir C programı oluşturalım.

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)>

Yukarıdaki programda da gördüğümüz gibi kullanıcıdan bir tam sayı aktardık. Verilen sayı negatifse, pozitif sayıyı döndürmek için (-1) ile çarpılacaktır. Ve eğer sayı pozitifse aynı sayıyı döndürür.