C Program to Find Absolute Value of a Number

In this post, we will learn how to find the absolute value of a number using C Programming language.

The absolute value of a number is its distance from 0. For example: the absolute value of 4 is 4 which is its distance from 0, but the absolute value of -4 is also 4.

In this program, we will see how to find the absolute value of an integer, a floating point number and a long integer using a pre-defined abs() function.

So, without further ado, let’s begin this tutorial.

C Program to Find Absolute Value of a Number

// C Program to Find Absolute Value of a Number
#include <stdio.h>
#include <stdlib.h>

int main(){
    int num, absolute;
    
    // Asking for input
    printf("Enter the number: ");
    scanf("%d", &num);
    
    absolute = abs(num);
    
    // Displaying output
    printf("Absolute value of %d is %d.", num, absolute);
    return 0;
}

Output

Enter the number: -45
Absolute value of -45 is 45.

How Does This Program Work ?

    int num, absolute;

In this program, we have declared two int data type variables named num and absolute.

    // Asking for input
    printf("Enter the number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number.

  absolute = abs(num);

We find the absolute value of this integer using the abs() function. The abs() function returns the absolute value of an integer. This function is defined under the stdlib.h header file.

    // Displaying output
    printf("Absolute value of %d is %d.", num, absolute);

Finally, the result is displayed on the screen using printf() function.

C Program to Find Absolute Value of Floating Point Number

// C Program to Find Absolute Value of a Number Using fabs() 
#include <stdio.h>
#include <math.h>

int main(){
    float num, absolute_num;
    
    // Asking for input
    printf("Enter a floating point number: ");
    scanf("%f", &num);
    
    absolute_num = fabs(num);
    
    // Displaying output
    printf("Absolute value of %.2f is %.2f", num, absolute_num);
    return 0;
}

Output

Enter a floating point number: -4484.12
Absolute value of -4484.12 is 4484.12

The fabs() function returns the absolute value of a floating point number. It is defined in the math.h header file.

C Program to Find Absolute Value of long Integer

// C Program to Find Absolute Value of a Number Using labs() 
#include <stdio.h>
#include <stdlib.h>

int main(){
    long int num, absolute_num;
    
    // Asking for input
    printf("Enter a long integer number: ");
    scanf("%ld", &num);
    
    absolute_num = labs(num);
    
    // Displaying output
    printf("The absolute value of %ld is %ld.", num, absolute_num);
}

Output

Enter a long integer number: -5464545
The absolute value of -5464545 is 5464545.

The labs() function is used to return the absolute value of a long integer number and it is also defined in the stdlib.h header file.

Conclusion

I hope after going through this post, you understand how to find the absolute value of a number using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *