×

C Math Library

C Math Library

The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as a result. As explained in the previous tutorial, here are more functions that can be used in <math.h> library

  1. double exp (double number)

The exp() returns the value of e raised to the power of the given number.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 int main()
 {
 double x = 12.0, result;
 result = exp(x);
 printf (“The exponential value of 12 is = %.2lf”, x, result);
 return 0;
 } 

Output

C Math Library
  1. double fmod (double number 1, double number 2)

It returns the remainder of the number 1 divided by the number 2. It works as a modulus function.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 int main ()
 {
 int a, b, c;
 a = 8;
 b = 6;
 c = 4;
 printf("The remainder of 8 / 4 is %lf \n", fmod(a, c));
 printf("The remainder of 8 / 6 is %lf \n", fmod(a, b));
 printf("The remainder of 6 / 4 is %lf \n", fmod(b, c));   
 return(0);
 } 

Output

C Math Library
  1. double acos (double number)

The acos() returns the arc cosine of the number given in radians.

E.g.:

 #include <stdio.h>
 #include <math.h>
 int main()
 {
 const double PI =  3.1415926;
 double x, result;
 x =  -0.5;
 result = acos(x);
 printf("the inverse of cos(-0.5) = %.2lf in radians\n", result);
 /* converting radians to degree */
 result = acos(x)*180/PI;
 printf("The inverse of cos(-0.5) = %.2lf in degrees\n", result);
 /* paramter not in range*/
 x = 1.2;
 result = acos(x);
 printf("Inverse of cos(1.2) = %.2lf", result);
 return 0;
 } 

Output

C Math Library
  1. double asin (double number)

The asin() returns the arc sine of the number given in radians.

E.g.:

 #include <stdio.h>
 #include <math.h>
 int main()
 {
 const double PI =  3.1415926;
 double x, result;
 x =  -0.5;
 result = asin(x);
 printf("the inverse of sine(-0.5) = %.2lf in radians\n", result);
 /* converting radians to degree */
 result = asin(x)*180/PI;
 printf("The inverse of sine(-0.5) = %.2lf in degrees\n", result);
 /* paramter not in range*/
 x = 1.2;
 result = asin(x);
 printf("The inverse of sine(1.2) = %.2lf", result);
 return 0;
 } 

Output

C Math Library
  1. double atan (double number)

The atan() returns the arc tangent of the number given in radians.

E.g.:

 #include <stdio.h>
 #include <math.h>
 #define PI 3.141592654
 int main()
 {
 double num = 1.0, result;
 result = atan(num);
 printf("The inverse of tan(1.0) = %.2f in radians \n", result);
 // Converting radians to degrees
 result = (result * 180) / PI;
 printf("the Inverse of tan(1.0) = %.2f in degrees \n", result);
 return 0;
 } 

Output

C Math Library
  1. double cos (double number)

The cos() returns the cosine of the number in radian.

E.g.:

 #include <stdio.h>
 #include <math.h>
 #define PI 3.14159265
 int main ()
 {
 double x, ret, val;
 x = 60.0;
 val = PI / 180.0;
 ret = cos( x*val );
 printf("The cosine of %lf is %lf degrees\n", x, ret);
 x = 90.0;
 val = PI / 180.0;
 ret = cos( x*val );
 printf("The cosine of %lf is %lf degrees\n", x, ret);       
 return 0;
 } 

Output

C Math Library
  1. double cosh (double number)

It returns the hyperbolic cosine of the value of the number.

E.g.:

 #include <stdio.h>
 #include <math.h>
 int main ()
 {
 double x, result;
 x = 0.5;
 result = cosh(x);
 printf("Hyperbolic cosine of 0.5 (in radians) = %lf\n", x, result);
 x = -0.5;
 result = cosh(x);
 printf("Hyperbolic cosine of -0.5 (in radians) = %lf\n", x, result);
 x = 0;
 result = cosh(x);
 printf("Hyperbolic cosine of 0 (in radians) = %lf\n", x, result);
 return 0;
 } 

Output

C Math Library
  1. double sin (double number)

It returns the sine of the number in radian.

E.g.:

 #include <stdio.h>
 #include <math.h>
 int main()
 {
 double x;
 double result;
 x = 2.3;
 result = sin(x);
 printf("The sin(2.3) = %.2lf \n", result);
 x = -2.3;
 result = sin(x);
 printf("The sin(-2.3) = %.2lf \n", result);
 x = 0;
 result = sin(x);
 printf("The sin(0) = %.2lf \n", result);
 return 0;
 } 

Output

C Math Library
  1. double sinh (double number)

It returns the hyperbolic sine of the value of the number.

E.g.:

 #include <stdio.h>
 #include <math.h>
 #define PI 3.141592654
 int main()
 {
 double angle = 2.50, result;
 result = sinh(angle);
 printf("Sine hyperbolic of 2.50 (in radians) = %.2lf", angle, result);
 return 0;
 } 

Output

C Math Library
  1. double tanh (double number)

It returns the hyperbolic tangent of the value of the number.

E.g.:

 #include <stdio.h>
 #include <math.h>
 int main ()
 {
 double x, result;
 x = 0.5;
 result = tanh(x);
 printf("The hyperbolic tangent of 0.5 is %lf degrees", result);
 return(0);
 } 

Output

C Math Library
  1. double modf (double number, double *integer)

It returned those value which will be the fraction component and sets the integer to the integer component.

E.g.:

 #include<stdio.h>
 #include<math.h>
 int main ()
 {
 double x, fractpart, intpart;
 x = 3.345;
 fractpart = modf(x, &intpart);
 printf("The integral part = %lf\n", intpart);
 printf("The fraction Part = %lf \n", fractpart);
 return(0);
 } 

Output

C Math Library

Related Topics

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.