×

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

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 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.

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.

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.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.