Find Absolute Value of a Number in C

0
68
Find absolute value of a number in C

Today, let’s learn how to find absolute value of a number in C. I am referring to an awesome book called “Let us C” by “Yashwant Kanetkar” and I will post solutions to the questions asked in exercises. This is the eighth program of the second chapter: The Decision Control Structure. Please come back for more programs.  If you are unsure about any part and more information, put it down in the comments section below or look it up on Google if you want a quick response. We try to answer all the queries as soon as possible.

Question:

Find the absolute value of a number entered through the keyboard.

Solution to Find Absolute Value of a Number in C:

This program is fairly easy. All you need to do is accept a number whose absolute value you want to find using the scanf() function. Then using the if-else conditional loop check if the number is positive or negative. If the number is positive, then the absolute value remains the same. If the number is less than 0 then multiply the number by -1 to make it positive.

Download the source code:

Download Program

Program to Find Absolute Value of a Number in C:

//C Program to Find Absolute Value of a Number in C by ReadMeNow
#include <stdio.h> 
#include <conio.h> 
  main() 
{
  int number;
  clrscr();
  printf("\n Enter any number to find the absolute value for - ");
  scanf("\n %d",&number);
  if(number<0)
{
  number=(-1)*number;
  printf("\n Absolute value is =%d",number);
}
  return (0);
}

Output

Enter any number to find the absolute value for – 78

Absolute value is 78

Download the source code:

Download Program

Read Also:

Introduction to C Programming

Multiplying two numbers in C

How to Calculate Simple Interest in C Programming

Calculate gross salary in C

Print ASCII Values Using While Loop in C Program