w3resource

C Exercises: Return the absolute value of a long integer

C Variable Type : Exercise-8 with Solution

Write a C program to return the absolute value of a long integer.

Sample Solution:

C Code:

#include<stdio.h>      // Include the standard input/output header file.
#include<stdlib.h>     // Include the standard library header file.

int main ()          // Start of the main function.
{
long int x, y;    // Declare two long integer variables 'x' and 'y'.

printf("\n Input 1st number (positive or negative) : ");   // Prompt the user to input the first number.
scanf("%ld",&x);   // Read the user's input and store it in 'x'.

printf(" Input 2nd number (positive or negative) : ");   // Prompt the user to input the second number.
scanf("%ld",&y);   // Read the user's input and store it in 'y'.

printf (" The absolute value of 1st number is : %ld\n",labs(x));   // Calculate and print the absolute value of 'x'.
printf (" The absolute value of 2nd number is : %ld\n\n",labs(y));   // Calculate and print the absolute value of 'y'.

return 0;   // Return 0 to indicate successful execution of the program.
}   // End of the main function.
	

Sample Output:

Input 1st number (positive or negative) : 25                                                                 
Input 2nd number (positive or negative) : -125                                                               
The absolute value of 1st number is : 25                                                                     
The absolute value of 2nd number is : 125 

Flowchart:

C Exercises Flowchart: Return the absolute value of a long integer

C Programming Code Editor:

Previous: Write a C program to integral quotient and remainder of a division.
Next: Write a C program to get the environment string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.