Ad Code

Responsive Advertisement

Math Function in C? With Example

 Math Function in C

C Programming allows us to perform mathematical operations through the functions defined in <math.h> header file. The <math.h> header file contains various methods for performing mathematical operations such as sqrt(), pow(), ceil(), floor() etc.

Math Functions

There are various methods in math.h header file. The commonly used functions of math.h header file are given below.

No.FunctionDescription
1)ceil(number)rounds up the given number. It returns the integer value which is greater than or equal to given number.
2)floor(number)rounds down the given number. It returns the integer value which is less than or equal to given number.
3)sqrt(number)returns the square root of given number.
4)pow(base, exponent)returns the power of given number.
5)abs(number)returns the absolute value of given number.

Math Function Examples

c
#include <stdio.h> #
include <math.h> 
int main() 
// Basic arithmetic operations
int a = 5, b = 3;
int sum, diff, product, quotient, remainder; 
 sum = a + b; 
 diff = a - b;
 product = a * b;
 quotient = a / b;
 remainder = a % b;
printf("Basic Arithmetic Operations:\n"); 
printf("Sum: %d\n", sum); 
printf("Difference: %d\n", diff); 
printf("Product: %d\n", product); 
printf("Quotient: %d\n", quotient); 
printf("Remainder: %d\n", remainder); 
// Power function 
double base = 2.0, exponent = 3.0
double result_pow = pow(base, exponent);
printf("\nPower Function:\n"); 
printf("Result of %.2lf raised to the power %.2lf: %.2lf\n", base, exponent, result_pow);
// Square root function 
double num_sqrt = 25.0
double squareRoot = sqrt(num_sqrt); 
printf("\nSquare Root Function:\n"); 
printf("Square root of %.2lf: %.2lf\n", num_sqrt, squareRoot);
// Trigonometric functions
double angle = 45.0
double radian = angle * M_PI / 180.0; // Convert angle to radians 
double sineValue = sin(radian); 
double cosineValue = cos(radian);
double tangentValue = tan(radian); 
printf("\nTrigonometric Functions:\n"); 
printf("Sine of %.2lf degrees: %.4lf\n", angle, sineValue); 
printf("Cosine of %.2lf degrees: %.4lf\n", angle, cosineValue); 
printf("Tangent of %.2lf degrees: %.4lf\n", angle, tangentValue); 
return 0
}
This program covers basic arithmetic operations, the power function, square root function, and trigonometric functions (sin, cos, tan). Compile and run this program to see the results of these mathematical operations.

Post a Comment

0 Comments