×

Math functions in C

Math functions in C: 

In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. 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. The <math.h> header file consist of various methods and types for performing many mathematical operations in the program, such as sqrt(), floor(), ceil(), pow(), and so on.

The C mathematical operations are a group of functions present in the standard library of the C standard language that implements essential functions of mathematics. In present, all these functions use floating point numbers in one manner or the other.

Different C standards provide a different albeit backward compatible set of functions. Most of the functions are available in the C standard library but in different header files.

Functions included in <math.h> header are as discussed below:

  • double ceil (double number)

The ceil() rounds up the given number, and it always returns the integer value, which will be greater than or equal to the given number.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
float n1, n2, n3, n4;
n1 = 1.6;
n2 = 1.2;
n3 = -1.6;
n4 = -1.2;
printf (“The ceil of n1 = %f \n”, ceil(n1));
printf (“The ceil of n2 = %f \n”, ceil(n2));
printf (“The ceil of n2 = %f \n”, ceil(n2));
printf (“The ceil of n2 = %f \n”, ceil(n2));
return 0;
} 

Output

Math Functions In C
  • double floor (double number):

The floor() rounds up the given number, and it always returns the integer value, sqrt() which will be greater than or equal to the given number.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
float n1, n2, n3, n4;
n1 = 1.6;
n2 = 1.2;
n3 = -1.6;
n4 = -1.2;
printf (“The floor of n1 = %f \n”, floor(n1));
printf (“The floor of n2 = %f \n”, floor(n2));
printf (“The floor of n3 = %f \n”, floor(n3));
printf (“The floor of n4 = %f \n”, floor(n4));
return 0;
} 

Output

Math Functions In C
  • double abs (double number)

The abs() returns the absolute value of the given number.

E.g.:

 #include <stdio.h>
#include <stdlib.h>
int main()
{
   int m = abs(200);     // m is assigned to 200
   int n = abs(-400);    // n is assigned to -400
   printf("Absolute value of m = %d\n", m);
   printf("Absolute value of n = %d \n",n);
   return 0;
} 

Output

Math Functions In C
  • double fabs (double number)

The fabs() and abs() function work in a similar way as both returns the absolute value of the number given.

  • double pow (base, exponent)

The pow() returns the power of the given number and its exponent.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int base, power;
double float;
printf (“Enter the base number:”);
scanf (“%d”, &base);
printf (“Enter the power to be raised of the number:”);
scanf (“%d”, &base);
result = pow(base, power);
printf (“%d ^ %d = %.2 lf”, base, power, result);
return 0;
} 

Output

Math Functions In C
  • sqrt (double number)

The sqrt() returns the square root of the given number.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
printf (“The square root of the number 5 is: %d”, sqrt(5));
printf (“The square root of the number 10 is: %d”, sqrt(10));
return 0;
} 

Output

Math Functions In C
  • double log (double number)

The log() returns the natural logarithm of the given number, that is, the base e logarithm of the number.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
double n = 5.6, result;
result = log(num);
printf (“log(%.lf) = %.2 f”, num, result);
return 0;
} 

Output

Math Functions In C
  • double log10 (double number)

The log10() returns the common logarithm of the given number, that is, the base 10 logarithm of the number.

E.g.:

 #include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
double n = 4, result;
result = log10(num);
printf (“log10(%lf) = %.2 f”, num, result);
return 0;
} 

Output

Math Functions In C

Related Topics

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.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

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.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.