w3resource logo


C Programming Exercises

C Exercises: Return the absolute value of an integer

Secondary Nav

C Variable Type : Exercise-16 with Solution

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

Test Data and Expected Output :

Input a positive or negative number :-25                               
 The absolute value of the given number is : 25

C Code:

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

int main ()
	{
		int num;
		printf("\n Input a positive or negative number :");
		scanf("%d",&num);
		printf (" The absolute value of the given number is : %d\n\n",abs(num));
		return 0;
	}

Solution

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

int main ()
	{
		int num;
		printf("\n Input a positive or negative number :");
		scanf("%d",&num);
		printf (" The absolute value of the given number is : %d\n\n",abs(num));
		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