×

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

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

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.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

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

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

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

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.