C Program to find absolute value

Absolute value means remove negative sign if any in the front of number. If the number is 5 then absolute value of 5 is 5. If number is -5 then absolute value of -5 is 5.
Simple C Program to find absolute value
#include<stdio.h>
int main()
{
 int n;
 printf("Enter anumber: ");
 scanf("%d",&n);
 if(n<0)
 {
  n=-n;
 }
 printf("Absolute value = %d",n);
 return 0;
}
C Program to find absolute value using while loop
#include<stdio.h>
int main()
{
 int n;
 printf("Enter anumber: ");
 scanf("%d",&n);
 while(n<0)
 {
  n=-n;
  break;
 }
 printf("Absolute value = %d \n",n);
 return 0;
}
C Program to find absolute value using for loop
#include<stdio.h>
int main()
{
 int n;
 printf("Enter anumber: ");
 scanf("%d",&n);
 for(;n<0;)
 {
  n=-n;
 }
 printf("Absolute value = %d \n",n);
 return 0;
}

C program to find absolute value using abs() function
C library function int abs(int x) returns absolute value of a number.
#include<stdio.h>
#include<stdlib.h>
int main()
{
 int n;
 printf("Enter anumber: ");
 scanf("%d",&n);
 n=abs(n);
 printf("Absolute value = %d \n",n);
 return 0;
}

Popular posts from this blog