abs Arithmetic function in C programming language
- Home
- function in C
- abs Arithmetic function in C programming language
- On
- By
- 0 Comment
- Categories: function in C, Library function in C, pre-define maths function in C
abs Arithmetic function in C programming language
abs Arithmetic function in C programming language
We will learn about abs Arithmetic function in C programming language
In the C Programming Language, the abs() Arithmetic function returns the absolute value of an integer. The absolute value of a number is always positive.
abs Arithmetic function
Declaration
Syntax
The syntax of the abs() function in the C programming Language
int abs(int x);
parameters or Argument
x is the parameter or argument of this function.
Return value
When a value for x is passed, this function returns the absolute value of x
Required Header
In the C Language, the required header file for abs() function is
#include <stdlib.h>
Example
Program 1
The following example explains the usage of abs() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=10,b=-12;
printf("the absolute value of a: %d\n",abs(a));
printf("the absolute value of b: %d\n",abs(b));
getch();
return 0;
}
When the above program is compiled and run, it will produce the following result
the absolute value of a: 10
the absolute value of b:12
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n,o,p;//diclare of local variable
printf("Enter the positive number\n");
//get input positive
scanf("%d",&n);
printf("Enter the Negative number\n");
//get input negative
scanf("%d",&o);
m=abs(n);//assign absolute value of n to m
p=abs(o);//assign absolute value of o to p
printf("absolute value of the positive value is: %d\n",m);
printf("absolute value of the Negative value is: %d",p);
getch();
return 0;
}
When the above program is compiled and run, it will produce the following result
Enter the positive number
56
Enter the Negative number
-76
the absolute value of the positive value is: 56
the absolute value of the Negative value is: 78
Similar Functions
Other C programming language functions that are similar to the cosine function
User defined function in C language
Arithmetic functions in C Language