×

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.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

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

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute 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.

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.