by Dinesh Thakur Category: Control Structures

These statements determine and print the absolute value of variable n. If n is negative, then n < 0 evaluates as true and n is assigned the value of -n, which is then printed in the next statement assuming variable n to be of some integer type. However, if the value of n is positive, it remains unchanged and is printed.

Note that the above if statement works with both integral and floating point types. Recall that we can also use the abs and fabs functions to determine the absolute value of an integer and floating point expression, respectively.

Illustrates the determination of absolute values

#include <stdio.h>

#include <math.h>
void main()
  int n = -765;
  long int m = - 786548;
  float x = -78.0;
  double y = -67.78654;
  long double z = - 564.564327;
  printf("abs(n)= %d,\t labs(m) = %d\n", abs(n), labs(m));
 printf("fabs(x)= %f\nfabs(y) = %f\nfabs(z)= %f\n", fabs(x), fabs(y),fabsl (z) );
}   C Program find the absolute value of a given Number

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles