Write a C program to find the absolute value of the given number entered by user. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Write a C program to find the absolute value of the given number entered by user.

I couldn't have the idea to how to get absolute value of a number entered by user either negative or positive. If negative number entered then we have to change it in positive of it but how... Please explain programmers.... I want to know

2nd Jul 2018, 8:07 AM
Rahul Gupta
Rahul Gupta - avatar
9 Answers
+ 1
You can use the abs() function from stdlib.h or fabs() from math.h for getting |x| of a number x : x=abs(x), or x=fabs(x); Alternatively, you can do : if(x<0) x-=(2*x); // Saves |x| into x.
2nd Jul 2018, 8:41 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Can you post your code?
2nd Jul 2018, 8:30 AM
KrOW
KrOW - avatar
+ 2
What is your problem? Get user input, calculate the absolute number or either?
2nd Jul 2018, 8:15 AM
KrOW
KrOW - avatar
+ 2
x= x<0 ?-x: x;
2nd Jul 2018, 9:24 AM
KrOW
KrOW - avatar
+ 1
Yes thats my problem
2nd Jul 2018, 8:20 AM
Rahul Gupta
Rahul Gupta - avatar
+ 1
#include<stdio.h > int main() { int n; printf("Enter a number:") ; scanf("%d", &n) ; //After that what I do }
2nd Jul 2018, 8:44 AM
Rahul Gupta
Rahul Gupta - avatar
+ 1
Thanks buddy.... Kinshuk Vasisht
2nd Jul 2018, 8:45 AM
Rahul Gupta
Rahul Gupta - avatar
+ 1
I uses alternative because I have to use conditional operator
2nd Jul 2018, 9:03 AM
Rahul Gupta
Rahul Gupta - avatar
+ 1
Thanks KrOW
2nd Jul 2018, 10:59 AM
Rahul Gupta
Rahul Gupta - avatar