C - Tutorial
     

C - Maths

Math

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

C Math Functions

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

Function Description
ceil(number) rounds up the given number. It returns the integer value which is greater than or equal to given number.
floor(number) rounds down the given number. It returns the integer value which is less than or equal to given number.
sqrt(number) returns the square root of given number
pow(base, exponent) returns the power of given number.
abs(number) returns the absolute value of given number.

C ceil()

The ceil() function computes the nearest integer greater than the argument passed. C ceil() Prototype.

double ceil( double arg );

The ceil() function takes a single argument and returns a value of type int.

For example: If 2.3 is passed to ceil(), it will return 3.

The function is defined in <math.h> header file.

long double ceill( long double arg );

float ceilf( float arg );

In order to find the ceil() of long double or float, you can use the above prototype.

Example: C ceil() function

#include <stdio.h>

#include <math.h>

int main()

{

double num = 8.33;

int result;

result = ceil(num);

printf("Ceiling integer of %.2f = %d", num, result);

return 0;

}

Output:

Ceiling integer of 8.33 = 9

C floor()

The floor() function calculates the nearest integer less than the argument passed.

C floor() Prototype.

double floor(double arg)

The floor() function takes a single argument and returns the value in type double.

It is defined in header file.

For example:

If 2.3 is passed to floor(), it will return 2.

In order to calculate floor() for long double or float, you can use the following prototype.

long double floorl( long double arg );

float floorf( float arg );

Example: C floor() function

#include <stdio>

#include <math.h>

int main()

{

double num = -8.33;

int result;

result = floor(num);

printf("Floor integer of %.2f = %d", num, result);

return 0;

}

Output

Floor integer of -8.33 = -9

C sqrt()

The sqrt() function computes the square root of a number.

Function prototype of sqrt()

double sqrt(double arg);

The sqrt() function takes a single argument (in double) and returns its square root (also in double).

[Mathematics] √x = sqrt(x) [In C Programming]

The sqrt() function is defined in math.h header file.

You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.

long double sqrtl(long double arg );

float sqrtf(float arg );

Example: C sqrt() Function

#include <math.h>

#include <stdio.h>

int main() {

double number, squareRoot;

printf("Enter a number: ");

scanf("%lf", &number);

// computing the square root

squareRoot = sqrt(number);

printf("Square root of %.2lf = %.2lf", number, squareRoot);

return 0;

}

Output

Enter a number: 23.4

Square root of 23.40 = 4.84

C pow()

The pow() function computes the power of a number.

The pow() function takes two arguments (base value and power value) and, returns the power raised to the base number. For example,

[Mathematics] xy = pow(x, y) [In programming]

The pow() function is defined in math.h header file.

C pow() Prototype

double pow(int x, int y)

The first argument is a base value and second argument is a power raised to the base value.

Example: C pow() function

#include <stdlib.h>

#include <stdio.h>

int main()

{

int base, power, result;

printf("Enter the base number: ");

scanf("%d", &base);

printf("Enter the power raised: ");

scanf("%d",&power);

result = pow(base,power);

printf("%d^%d = %d", base, power, result);

return 0;

}

Output:

Enter the base number: 8

Enter the power raised: 2

8^2 = 64

c abs() function

The C library function int abs(int x) returns the absolute value of int x.

Following is the declaration for abs() function.

int abs(int x)

x − This is the integral value.

Example: C pow() function

#include <math.h>

#include <stdio.h>

int main () {

int a, b;

a = abs(5);

printf("value of a = %d\n", a

b = abs(-10);

printf("value of b = %d\n", b);

return(0);

}

Let us compile and run the above program, this will produce the following result -

output:

value of a = 5

value of b = 10

www.reshman.online