C Programming

Math Functions in C – Complete Guide

Are you looking to improve your C programming skills and learn more about mathematical functions? Look no further! In this article, we will dive into the world of math functions in C programming and explore the various functions available to help you solve complex mathematical problems. Whether you’re a beginner or an experienced programmer, understanding these functions is essential to building efficient and accurate programs.

What are Mathematical Functions in C?

Mathematical functions in C refer to a collection of built-in functions that facilitate the execution of different mathematical operations on numerical data. These functions range from basic arithmetic operations such as addition, subtraction, multiplication, and division to advanced ones such as trigonometric and logarithmic functions.

List Mathematical Functions in C?

There are many mathematical functions in C that programmers can use for various calculations. Some of these mathematical functions are listed below.

  1. floor(): The closest integer which is either smaller or equal to the parameter provided is found by this function.
  2. ceil(): This function returns the closest integer value that is higher or equal to the argument passed to it.
  3. round(): This function returns the float, double, or large double argument’s nearest integer value. Whenever the decimal value falls between “.1” and “.5”, it returns an integer value that is less than the given. Whenever the decimal value falls between “.6” and “.9,” an integer value that is larger than the parameter is returned.
  4. fmod(): When a number is divided by another number, this function returns the remainder for the given two input values.
  5. Pow(): This serves to calculate the power of a specified number.
  6. sqrt(): This function calculates a square root of the passed parameter to it.
  7. trunc(): This function returns the integer value after truncating the decimal value from the floating point value.
  8. log(): This function computes the natural logarithm.
  9. log10(): This function finds the logarithm value of base 10.
  10. sinh(): This function computes the hyperbolic sine.
  11. cosh(): This function computes hyperbolic cosine.
  12. tanh(): This function computes the hyperbolic tangent.
  13. sin(): This function computes the sine value.
  14. cos(): This function determines the cosine value.
  15. tan(): This function determines the tangent value.
  16. exp(): This function computes the exponential “e” to the xth power.

How to Use Mathematical Functions in C?

Here are the steps to use Mathematical Functions in C:

Step 1: Your C program must initially contain the math.h header file, which provides declarations for each of the mathematical functions.

Step 2: Declare the variables that you want to perform mathematical operations on.

Step 3: Call the appropriate mathematical function for the operation you want to perform. Pass the variables as a parameter to the function.

Step 4: After performing the operation using the mathematical function, print the result to the console.

Here is an example that takes input from the users and performs calculations using several mathematical functions.

#include <stdio.h>

#include <math.h>

int main()

{

    float num;
    printf("Please enter the float number:\n");
scanf("%f", &num);
    int f=floor(num);
    printf("Floor value of %f = %d\n",num, f);
    float s=sqrt(num);
    printf("Square root value of %f = %f\n",num, s);
    float p=pow(num,5);
     printf("power calculated of %f = %f\n",num, p);
    int t=trunc(num);
     printf("truncated value of %f = %d\n",num, t);
    float e=exp(num);
    printf("Exponential value of %f = %f\n",num, e);
    int c=ceil(num);
    printf("Ceil value of %f = %d\n",num, c);
    int r=round(num);
    printf("Round of value of %f = %d\n",num, r);
    float ln=log(num);
    printf("Natural log of %f = %f\n",num, ln);
    int l=log10(num);
    printf("Common log of %f = %d\n",num, l);
    float sn=sin(num);
    printf("Sine value of %f = %f\n",num, sn);
    float sh=sinh(num);
    printf("Sine hyperbolic value of %f = %f\n",num, sh);
    float cn=cos(num);
    printf("Cosine value of %f = %f\n",num, cn);
    float ch=cosh(num);
    printf("Cosine hyperbolic value of %f = %f\n",num, ch);
    float tn=tan(num);
     printf("Tangent value of %f = %f\n",num, tn);
    float th=tanh(num);
    printf("Tangent hyperbolic value of %f = %f\n",num, th);
    int num1=14;
    int num2=3;
    int rem=fmod(num1,num2);
printf("Remainder value = %d\n",num, rem);
    return 0;

Conclusion

Understanding mathematical functions in C is crucial for any programmer who wants to build efficient and accurate programs. By leveraging built-in functions like floor, ceil, round, fmod, pow, sqrt, trunc, log, log10, sinh, cosh, tanh, sin, cos, tan, and exp, programmers can perform complex mathematical calculations with ease and precision. By following the simple steps outlined in this article, programmers can include these functions in their code and improve the efficiency of their mathematical computations.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.