×

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if we give -10 as input, it returns 10. If we give a positive number, it returns the same number. To use abs() function in the program, we must include stdlib.h header file.

Syntax :

a= abs(number);

Format of abs() function: int abs( int n );

It takes an integer value as the parameter and returns the positive number.

Advantages of abs() function:

  1. It converts the negative value into a positive value.
  2. Lines of code get reduced.
  3. Easy to learn.
// sample program to find the absolute value of given number using abs() function
#include <stdio.h>  
#include <stdlib.h> // including the header file 
int main()  
{  
    int number, abslute_value; // declare the local variable  
    printf (" Enter the number to convert it into absolute  value: ");
    scanf ("%d", &number);  // reading the input
    abslute_value= abs (number);  // function call
    // printing the absalute value
    printf ("\n The absolute value of %d is %d. ", number, abslute_value); 
    return 0;
}  

Output: Test case 1

Enter the number to convert it into absolute  value: -100
The absolute value of -100 is 100.

In this test case, we gave a negative number as input, and it returned a positive number.

Output: Test case 2

Enter the number to convert it into absolute  value: 100
The absolute value of 100 is 100.

In this test case we gave positive number as input and it returned positive number.

// sample program to find the absolute value of given number without using abs() function
#include <stdio.h>  
#include <stdlib.h> // including the header file 
int main()  
{  
    int number, abslute_value; // declare the local variable  
    printf (" Enter the number to convert it into absolute  value: ");
    scanf ("%d", &number);  // reading the input
    abslute_value= -1*number;  // function call
    // printing the absalute value
    printf ("\n The absolute value of %d without using abs() function is %d. ", number, abslute_value); 
    return 0;
}  

Output:

Enter the number to convert it into absolute  value: -10
The absolute value of -10 without using abs() function is 10.

Example to find the square root of the given number.

// program to find square root of the given number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
    int number, absalute_value, square_root; // variable  declaration
    printf("Enter the number: ");
    scanf("%d", &number); // reading the number
    // calculating the absalute value
    absalute_value = abs( number ); // function call
    printf(" \nThe absalute value of the given number is: %d",absalute_value); // printing the absalute value
    // calculating the square root value
    square_root = sqrt(absalute_value); // function call
    printf(" \nThe square root value of the given number: %d",square_root); // printing square root value
    return 0;

Output:

Enter the number: -16
The absolute value of the given number is: 16 
The square root value of the given number: 4

Example program of abs() function on arrays:

// program to print the absalute values of the given array
#define max 100
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n, a[max], i, b[max];
    printf(" Enter the size of the array: ");
    scanf(" %d", &n); // reading the array size
    // Reading the array elements
    printf(" \nEnter the array elements: ");
    for( i=0; i<n; i++)
    {
        scanf(" %d", &a[i]); 
    }
    // finding the absalute value
      for( i=0; i<n; i++)
    {
        b[i] = abs ( a[i] ); // function call
    }
    // printing the absalute values
      for( i=0; i<n; i++)
    {
        printf(" \nThe absolute value of %d is  %d",a[i],b[i]);
    }
    return 0;
}

Output:

Enter the size of the array: 10
 Enter the array elements: 1 2 3 4 5 -6 -7 -8 -9 -10
 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 
The absolute value of -6 is  6 
The absolute value of -7 is  7 
The absolute value of -8 is  8 
The absolute value of -9 is  9 
The absolute value of -10 is  10

Example program to find absolute values of the array without using abs() function:

// program to print the absalute values of the given array with out using abs() function
#define max 100
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n, a[max], i, b[max];
    printf(" Enter the size of the array: ");
    scanf(" %d", &n); // reading the array size
    // Reading the array elements
    printf(" \nEnter the array elements: ");
    for( i=0; i<n; i++)
    {
        scanf(" %d", &a[i]); 
    }
    // finding the absalute value
      for( i=0; i<n; i++)
    {
        if(a[i]<0)
        {
            b[i] = a[i] - 2*a[i];
        }
        else
        {
            b[i] = a[i];
        }
    }
    // printing the absalute values
      for( i=0; i<n; i++)
    {
        printf(" \nThe absolute value of %d is  %d",a[i],b[i]);
    }
    return 0;
}

Output:

Enter the size of the array: 10
Enter the array elements: 10 20 30 40 50 -60 -70 -80 -90 -100
The absolute value of 10 is  10 
The absolute value of 20 is  20 
The absolute value of 30 is  30 
The absolute value of 40 is  40 
The absolute value of 50 is  50 
The absolute value of -60 is  60 
The absolute value of -70 is  70 
The absolute value of -80 is  80 
The absolute value of -90 is  90 
The absolute value of -100 is  100

Conclusion:

In this article we learned about abs() function in C. The definition of abs() function, advantages of abs() function, examples on abs() function.


Related Topics

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.