+ 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
9 Réponses
+ 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.
+ 3
Can you post your code?
+ 2
What is your problem? Get user input, calculate the absolute number or either?
+ 2
x= x<0 ?-x: x;
+ 1
Yes thats my problem
+ 1
#include<stdio.h >
int main()
{
int n;
printf("Enter a number:") ;
scanf("%d", &n) ;
//After that what I do
}
+ 1
Thanks buddy.... Kinshuk Vasisht
+ 1
I uses alternative because I have to use conditional operator
+ 1
Thanks KrOW