EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Math Functions in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Math Functions in C

Math Functions in C

Introduction to Math Functions in C

This article lists the different mathematical functions used in C programming languages with working code illustration. Computers do huge mathematical calculations and analyses of huge numbers, to do so we have used math features in C. Before Starting with, we need to know the C languages use header/library called Math.h for various mathematical functions. This helps in calculating trigonometric operations, logarithms, absolute values, square roots. So, let us explore the different types of functions used in this library. All these functions take double as a data type and return the same.

Various Math Functions in C

Let’s see various functions defined in math.h and the Math library is categorized into three main types: Trigonometric functions, math functions, Log/expo functions. To implement the below functions, it is mandatory to include<cmath.h> or <math.h> in the code.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. floor (double a)

This function returns the largest integer value not greater than ‘a’ value. It rounds a value and returns a double as a result. It behaves differently for negative numbers, as they round to the next negative number.

Ex: floor (7.2) is 7.0
floor (-7.2) is -8.0

Example:

This program illustrates how to compute the floor for the declared value and rounds to the next value 10.

#include <stdio.h>
#include <math.h>
int main()
{
double f= -9.33;
int final;
final = floor(f);
printf("Floor value of %.2f = %d", f, final);
return 0;
}

Output:

floor (double a)

2. ceil ()

Syntax: 

double ceil (double b)

This function returns the smallest integer value that is greater or equal to b and rounds the value upwards. For a negative value, it moves towards the left. Example 3.4 returns -3 has the output.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Example:

This program explains by taking input in the float argument and returns the ceil value.

#include <stdio.h>
#include <math.h>
int main()
{
float n, ceilVal;
printf("  Enter any Numeric element :  ");
scanf("%f", &n);
ceilVal = ceil(n);
printf("\n The  Value of %.2f = %.4f ", n, ceilVal);
return 0;
}

Output:

ceil ()

3. Sqrt ()

This function returns the square root of a specified number.

Syntax:

sqrt( arg)

Example:

The below code explains the most known mathematical function sqrt() by taking ‘n’ values to compute the square root for the different ‘n’ values.

#include <stdio.h>
#include <math.h>
int main()
{
double n,output;
printf("Enter a number\n");
scanf("%lf", &n);
output = sqrt(n);
printf("Square root of %.2lf = %f", n,output);
return 0;

Output:

Sqrt ()

4. round ()

This function rounds the nearest value of a given input. It throws out the error if the value is too large. Other functions like lround (), llround () also rounds the nearest integer.

Syntax:

int round(arg)

Example:

The below code is very simple which does round off to the nearest ‘r’ value in the for loop.

#include <stdio.h>
#include <math.h>
int main ()
{
for(double r=110;r<=120;r+=1.1)
printf("round of  %.1lf is  %.1lf\n", r/5.0, round(r/5.0));
return 0;}

Output:

round ()

5.pow ()

This function returns to power for the given number(ab). It returns a raised to the power of b, which has two parameters base and exponent.

Example:

In the Below source code, we are allowing a user to enter an input value to compute the power of the given two arguments.

#include <stdio.h>
#include <math.h>
int main()
{
int r, ba, expr;
printf("\n Enter the Base and Exponent numbers :  \n");
scanf("%d %d", &ba, &expr);
r = pow(ba, expr);
printf("\n The result of %d Power %d = %d ", ba, expr ,r);
return 0;
}

output:

pow ()

6. trun()

This function helps in truncating the given value. It returns integer values. To truncate floating and double values truncf (), truncl () are used.

Syntax:

double trunc(a);

Example:

Below source code takes two input values a, b to truncate the double values.

#include <stdio.h>
#include <math.h>
void main() {
double m, n, a, b;
a = 56.16;
b = 85.74;
m = trunc(a);
n = trunc(b);
printf("The value of a: %lf\n",m);
printf("The value of a: %lf\n",n);
}

Output:

trun()

7. fmod()

This function returns the remainder for the given two input values when m divided by n.

Syntax:

double fmod(double I, double j)

Example:

In the below example it takes two values from the user to compute the remainder using fmod() function.

#include<stdio.h>
#include<math.h>
int main(){
double fiN;
double secN;
double n;
printf("Enter the first number : ");
scanf("%lf",&fiN);
printf("Enter the second number : ");
scanf("%lf",&secN);
printf("fmod(firstNumber,secondNumber) is %lf \n",fmod(fiN,secN));
}

Output:

fmod()

Trigonometric Functions

Below are the different functions of Trigonometric:

1. sin()

This built-in function gives sine value of the given number, calculates floating-point values. asin() computes arc, for hyperbolic it is sinh().

Syntax:

return type sin(y);

y returns value in radians and return type takes double.

Example:

In the following source code, I have taken two different input values to calculate sin value and returns double.

#include <stdio.h>
#include <math.h>
int main()
{
double a;
double z;
a = 4.3;
z = sin(a);
printf("sin(%.2lf) = %.2lf\n", a, z);
a = -4.3;
z = sin(a);
printf("sin(%.2lf) = %.2lf\n", a, z);
a = 45;
z = sin(a);
printf("sin(%.2lf) = %.2lf\n", a, z);
return 0;
}

Output:

trigo function c-1

2. sinh()

This math function computes trigonometric tangent sine value for the given number.

Syntax:

double sinh(x);

Example

In the below source code Sine hyperbolic is calculated by declaring an input value.

#include <stdio.h>
#include <math.h>
#define PI 3.141592654
int main()
{
double gt = 3.60, z;
z = sinh(gt);
printf("Sine hyperbolic of %.2lf is = %.2lf", gt, z);
return 0;
}

Output

trigo function c-2

3. cos()

This math function determines the trigonometric cosine value for the given element.

Syntax:  return type cos(argument);

#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
double cVal, rVal, dVal;
for(int i=0;i<=2;i++)
{
printf(" Enter an Angle in degrees :  ");
scanf("%lf", &dVal);
rVal = dVal * (PI/180);
cVal = cos(rVal);
printf("\n  The Cosine value of %f = %f ", dVal, cVal);
printf("\n");
}
return 0;
}

Output:

trigo function c-3

4. cosh()

It returns hyberbolic cosine for a given value.

Syntax:

double cosh(y);

Example

The below example shows it takes two different input values to compute hyperbolic.

#include <stdio.h>
#include <math.h>
int main ()
{
double k, r;
k = 0.6;
r = cosh(k);
printf("Hyperbolic cosine of %lf is = %lf\n", k, r);
k = -0.8;
r = cosh(k);
printf("Hyperbolic cosine of %lf is = %lf\n", k, r);
return 0;}

Output

trigo function c-4

5. tan()

This math library function calculates tangent values of the angle for the mathematical expression and measured in radians.

It can be declared as

double tan(arguments);

Example

In the following source code, tan value is calculated for the following angles which is incremented using for loop.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
float z ;
int k ;
char ch ;
printf("\nAngle \t Tan \n") ;
for (k = 0; k <= 180; k = k + 30)
{
z = k * 3.14159 / 180 ;
printf("\n %d, %5.2f",k, tan(z));
}
getch() ;
}

Output:

trigo function c-5

6. tanh()

tanh() function returns hyperbolic tangent of the given value. It takes a single parameter. In addition to find tangent for long double and float tanhl() and tanhf () are used for computation.

Syntax:

double tanh( val);

Example:

A tangent hyberbolic is calculated for ‘ j’ values using for loops. Let’s see how it works.

#include <stdio.h>
#include <math.h>
#define PI 3.141592654
int main()
{
double val,  r;
for(double j=0.60; j<=2.0;j+=.30)
{
r = tanh(j);
printf("Tangent hyperbolic of %.2lf is = %.2lf",j, r);
printf("\n");
}
return 0;
}

Output:

trigo function c-6

Log Arithmetic Functions

Below are the different functions of log arithmetic:

1. exp()

This function does computation on exponential for a given value(ex). There are also other subtypes like frexp(), Idexp() returning mantissa and multiplied to the power of x.

Syntax: 

return type exp(value);

Example:

The program takes numeric value from the user to compute the exponent for a given value and returns double.

#include <stdio.h>
#include <math.h>
int main()
{
double numb, eVal;
printf(" Enter any Numeric Value :  ");
scanf("%lf", &numb);
eVal = exp(numb);
printf("\n Exponential Value of e power %lf = %lf ", numb, eVal);
printf("\n");
return 0;
}

Output

Log arithmetic functions 1

2. log()

This function returns the logarithm value of a given number. (to the base e. loge)

Syntax: 

double log(arg);

Example:

In the following example, log value for the given number is calculated using function. User-defined function lgm() does computation and function is called in the main function.

#include<stdio.h>
#include<math.h>
float lgm ( float iv );
int main ()
{
float q, r ;
printf ( "\nEnter a number to find log value \n");
scanf ( "%f", &q ) ;
r = lgm ( q ) ;
printf ( "\nthe log value is %f is %f",q,r );
}
float lgm ( float iv )   // function definition
{
float exe ;
exe = log(iv);
return ( exe ) ;
}

output:

Log arithmetic functions 2 

Conclusion

To conclude, we have seen different mathematical functions used in C programming and these are the direct library functions to use. C programs utilize these functions for various mathematical operations. To solve some complex versions of computations this built-in function benefits mathematically oriented programming language to return simple values.

Recommended Articles

This is a guide to Math Functions in C. Here we discuss different mathematical functions in C with examples. You can also go through our other suggested articles –

  1. PHP Math Functions
  2. JavaScript Math Functions
  3. JavaScript String Functions
  4. Math Functions in Python

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More