ADVERTISEMENT
ADVERTISEMENT

Home » C programs » math.h header file functions

abs() and sqrt() functions of math.h in C

In this article, we are going to know about the abs() and sqrt() functions of math.h header file in C language and learn the process to use them.
Submitted by Manu Jemini, on March 17, 2018

Mathematics is something that always easy if you know the correct trick. So here we are learning the most common trick to solve some important calculations like finding the absolute value and getting the square root of a number.

To calculate the square root of a number you should use the function sqrt(), which will return a number representing the square root of the number that you have passed.

Similarly, you can use the abs() function which will return a number representing the absolute value of a number.

These functions are from the math.h file, which should be included in the program to use these functions. Below, you got two example for each one, if you want more clarification.

math.h - abs() function Example in C



#include <stdio.h>
#include <math.h>

int main()
{
	// set the type of variable
	int number, a;

	// message for user
	printf("Please enter a number from keyboard to calculate the absolute value\n");
	scanf("%d", &number);

	// storing the absolute value
	a = abs(number);

	// printing the calculated value
	printf("Calculated absolute value is : %d\n", a);

	return 0;
}

Output

math.h abs() in c language

math.h - sqrt() function Example in C



#include <stdio.h>
//to use 'sqrt()' function
#include <math.h>

int main()
{
	// set the type of variable
	float number, a;

	// message for user
	printf("Please enter a number from keyboard to calculate the square root of\n");
	scanf("%f", &number);

	// storing the square root of entered number
	a = sqrt(number);

	// printing the calculated value
	printf("Calculated square root value is : %.2lf\n", a);

	return 0;
}

Output

math.h sqrt() in c language

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.