w3resource logo


C Programming Exercises

C Exercises: Return the absolute value of a long integer

Secondary Nav

C Variable Type : Exercise-8 with Solution

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

Test Data and Expected Output :

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

C Code:

#include<stdio.h>
#include<stdlib.h>

	int main ()
	{
		long int x,y;
	      printf("\n Input 1st number (positive or negative) : ");
	      scanf("%ld",&x);	
	      printf(" Input 2nd number (positive or negative) : ");
	      scanf("%ld",&y);		
		printf (" The absolute value of 1st number is : %ld\n",labs(x));
		printf (" The absolute value of 2nd number is : %ld\n\n",labs(y));

		return 0;
	}

Solution

#include<stdio.h>
#include<stdlib.h>

	int main ()
	{
		long int x,y;
	      printf("\n Input 1st number (positive or negative) : ");
	      scanf("%ld",&x);	
	      printf(" Input 2nd number (positive or negative) : ");
	      scanf("%ld",&y);		
		printf (" The absolute value of 1st number is : %ld\n",labs(x));
		printf (" The absolute value of 2nd number is : %ld\n\n",labs(y));

		return 0;
	}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program