Abs Function In C - C Programming Tutorial
C Course / Programs / Abs Function In C

Abs Function In C

BLUF: Understanding Abs Function In C is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: Abs Function In C

C provides direct access to memory and system resources. Learn how Abs Function In C leverages this power in the lesson below.

Syntax

Example

int abs (int x);

In the given syntax, x represents an integer data type capable of storing negative or positive numerical values. It is supplied as an argument to the abs function to retrieve the absolute (positive) value due to the function's integer data type constraint.

Note: The abs function always returns a positive number even if the given number is either negative or positive.

Program to get the absolute value of a number using the abs function

Let's explore an instance demonstrating how to display the absolute value using the abs function within a C program.

Prog.c

Example

#include <stdio.h>
#include <stdlib.h>  // 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 ("\n The absolute value of %d is %d. ", num, n);
	return 0;
}

Output

Output

Enter a number to display the absolute value: -35

 The absolute value of -35 is 35.

Program to print the absolute values of the given integers using abs function

Let's develop a program that displays the absolute values of provided numbers by utilizing the abs function in the C programming language.

Absolute.c

Example

#include <stdio.h>
#include <stdlib.h>  // use stdlib.h header file to use abs() function.
#include <math.h>
int main()
{ 
	printf (" The absolute value of 27 is %d ", abs (27)); 
	printf (" \n The absolute value of -16 is %d ", abs (-16));
	printf (" \n The absolute value of -125 is %d ", abs (-125));
		
	printf (" \n The absolute value of 18 is %d ", abs (18));
	printf (" \n The absolute value of -29 is %d ", abs (-29));
	printf (" \n The absolute value of 0 is %d ", abs (0));
	
	return 0;
}

Output

Output

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 to print the absolute values between two integers using for loop

Let's explore an illustration demonstrating how to output the absolute difference between two integers using a for loop in a C program.

Abs2.c

Example

#include <stdio.h>
#include <stdlib.h>  
#include <math.h>
int main()
{	
	int i, num, last;
	printf (" Enter the first number: \n ");
	scanf (" %d", &num); 
	
	printf ("\n Enter the last number from which you want to get the absolute number: ");
	scanf (" %d", &last); 
	
	// use for loop to print the absolute number
	for (i = num; i <= last; i++)
	{
		// abs() function convert a negative number to positive number
		printf( "\n The absolute value of %d is %d. ", i, abs( i));
	}

	return 0;
}

Output

Output

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.

Program to get the absolute value without using the abs function

Let's develop a C program to calculate the absolute value of a number without relying on the abs function.

Abs.c

Example

#include <stdio.h>
#include <stdlib.h>  // 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
	{
		
		printf (" The absolute value is: %d", num);
	}
	return num;
}

int main()
{
	int num;
	printf (" Enter a number to display the absolute value: ");
	scanf ("%d", &num);
	
	// call the functon
	getAbsolute(num);
	return 0;
}

Output

Output

Enter a number to display the absolute value: -8
 The absolute value is: 8

In the code snippet shown above, an integer input is received from the user. In case the input is a negative number, it undergoes multiplication by (-1) to convert it into a positive number. Conversely, if the input is already positive, it remains unchanged.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience