Note: If you are using gcc, you would have to compile the C code with the following parameters in order to link the math library:
gcc filename.c -o outputfile -lm
C provides a robust set of mathematical functions through the <math.h> header file, enabling programmers to perform a wide range of mathematical operations. Here’s a detailed explanation of some commonly used C math functions, along with examples.
Power Function: pow()
Description: Raises a base to the power of an exponent.
Prototype:
double pow(double base, double exponent);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
printf("Power: %.2lf^%.2lf = %.2lf\n", base, exponent, result);
return 0;
}
Output:
Power: 2.00^3.00 = 8.00
Square Root: sqrt()
Description: Computes the square root of a number.
Prototype:
double sqrt(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
printf("Square root of %.2lf is %.2lf\n", num, sqrt(num));
return 0;
}
Output:
Square root of 16.00 is 4.00
Ceiling: ceil()
Description: Rounds a number up to the nearest integer.
Prototype:
double ceil(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = 4.3;
printf("Ceiling of %.2lf is %.2lf\n", num, ceil(num));
return 0;
}
Output:
Ceiling of 4.30 is 5.00
Floor: floor()
Description: Rounds a number down to the nearest integer.
Prototype:
double floor(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = 4.7;
printf("Floor of %.2lf is %.2lf\n", num, floor(num));
return 0;
}
Output:
Floor of 4.70 is 4.00
Absolute Value: fabs()
Description: Returns the absolute value of a floating-point number.
Prototype:
double fabs(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = -5.5;
printf("Absolute value of %.2lf is %.2lf\n", num, fabs(num));
return 0;
}
Output:
Absolute value of -5.50 is 5.50
Trigonometric Functions: sin(), cos(), tan()
Description: Computes the sine, cosine, and tangent of an angle (in radians).
Prototypes:
double sin(double x);
double cos(double x);
double tan(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double angle = 30.0; // Angle in degrees
double radians = angle * (M_PI / 180.0); // Convert to radians
printf("Sine of %.2lf degrees is %.2lf\n", angle, sin(radians));
printf("Cosine of %.2lf degrees is %.2lf\n", angle, cos(radians));
printf("Tangent of %.2lf degrees is %.2lf\n", angle, tan(radians));
return 0;
}
Output:
Sine of 30.00 degrees is 0.50
Cosine of 30.00 degrees is 0.87
Tangent of 30.00 degrees is 0.58
Exponential Function: exp()
Description: Computes the value of e raised to the power of x.
Prototype:
double exp(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double x = 1.0;
printf("e^%.2lf = %.2lf\n", x, exp(x));
return 0;
}
Output:
e^1.00 = 2.71
Logarithmic Functions: log(), log10()
Description: Computes the natural logarithm and base-10 logarithm.
Prototypes:
double log(double x);
double log10(double x);
Example:
#include <stdio.h>
#include <math.h>
int main() {
double num = 100.0;
printf("Natural log of %.2lf is %.2lf\n", num, log(num));
printf("Base-10 log of %.2lf is %.2lf\n", num, log10(num));
return 0;
}
Output:
Natural log of 100.00 is 4.61
Base-10 log of 100.00 is 2.00
Write a C program to find the largest number in an array.
Write a C program to reverse an array of integers.
Write a C program to sort an array of numbers in the ascending order.
Write a C program to find the mean, median and mode of elements in an array.
Write a C program to calculate the square root of each element in an array of integers using the sqrt() function, and then store the results in a new array? Ensure that the input array and the resulting array are accessible across different functions.
Write a C program which squares an array.
Write a program that reads an array of integers from the user and calculates the sin() and cos() of the sum of the array elements.