This blog is under construction

Monday, 8 July 2013

C program to find absolute value of a given number

Write a C program to find absolute value of a given number.


  #include <stdio.h>
  int main() {
        int val;

        /* get the input from the user */
        printf("Enter your input:");
        scanf("%d", &val);

        /* if the value is in negative, make it positive */
        if (val < 0) {
                val = val * (-1);
        }

        /* print the output */
        printf("Absolute value of the given number: %d\n", val);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:-100
  Absolute value of the given number: 100





See Also:

No comments:

Post a Comment