×

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

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

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.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.