forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
absolute_value.c
61 lines (49 loc) · 2.04 KB
/
absolute_value.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*******************************************************************************
*
* Program: Absolute Value Functions abs() And fabs()
*
* Description: Examples of finding the absolute value of a number using the
* abs() and fabs() functions in C. See the Wikipedia article on absolute
* value: https://en.wikipedia.org/wiki/Absolute_value.
*
* YouTube Lesson: https://www.youtube.com/watch?v=_GTWRamAQ0s
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
// We can think of the absolute value of a number as the non-negative value
// of that number, without regard to its sign. Or we can think of it as the
// "distance" of that number from 0. The absolute values (denoted by |value|)
// of 5, -5, 0 and -2.5 are given below:
//
// |5| = 5
// |-5| = 5
// |0| = 0
// |-2.5| = 2.5
#include <stdio.h>
// the abs() function is in the stdlib.h library so we must include it to use it
#include <stdlib.h>
// the fabs() function is in the math.h library so we must include it to use it
#include <math.h>
int main(void)
{
// Declare and initialize an int variable x to -5
int x = -5;
// abs() will accept an int value as an argument and return the absolute
// value of that int, so here we store the absolute value of x returned by
// abs() into the abs_x varible we have declared
int abs_x = abs(x);
// Output the absolute value of x that is stored into the abs_x variable
printf("abs(x): %d\n", abs_x);
// Declare and initialize a double variable y to -2.5
double y = -2.5;
// fabs() will allow us to find the absolute values of double/float values,
// as with abs() we pass the number as an argument to the function and the
// function will return the absolute value of that number. So here we
// have fabs() return the absolute value of y and we store it into the
// fabs_y double type variable we have declared.
double fabs_y = fabs(y);
// Output the absolute value of y that is stored in the abs_y variable
printf("fabs(y): %f\n", fabs_y);
return 0;
}