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:

1 comment:

  1. WAP to findout largest value in four digit number by using nested if else (without using ladder)?
    What type of condition using in this C program

    ReplyDelete