ankitweblogic logo
W e b t u t o r i a l s

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if , switch

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C - Math Function

The math.h header file defines various mathematical functions that can be used in our program.

Common 'C - Language' Math functions are:
S.No. Functions Syntax Description
1 abs() abs(value); Returns the absolute value of a integer number i.e. convert negative value into positive value.
2 fabs() fabs(float value); Returns the absolute value of a float number.
3 ceil() ceil(decimal value); Remove the decimal places and return the integer number. It returns one value greater to the original number.
4 floor() floor(decimal value); Remove the decimal places and return the integer number. It returns one value smaller to the original number.
5 fmod() fmod (x, y); Divide two float numbers and return the reminder.
6 modf() double a, b;
double c=modf(a,b);
Splits the double number into number and its decimal value.
Note: a,b,c should be in double.
7 pow() c = pow (a, b); Calculate a power b.
Note: a and b can be int, long, float, double.
8 sqrt() s=sqrt(n); Calculate the square root of a number.
Number can be int, long, float, double.
9 sin() sin (value); Calculate the sin value in radians.
Value should be in decimal.
10 cos() cos (value); Calculate the cos value in radians.
Value should be in decimal.
11 tan() tan (value); Calculate the tan value in radians.
Value should be in decimal.

Note: degree = radian * (180 / pi)

Example 1: Math abs() Function

void main()
{
  int a;
  printf("Enter number");
  scanf("%d",&a);
  printf("Absolute value = %d",a);
  getch();
}

Example 2: Math fabs() function.

void main()
{
  double a;
  printf("Enter number");
  scanf("%lf",&a);
  printf("Absolute value = %lf",a);
  getch();
}

Exercise Math Function

Exercise 1: Program to calculate Area of Triangle by using Math Library Function - sqrt().

#include<stdio.h>
#include<conio.h>

void main()
{
   float s,a,b,c,ar;
   clrscr();
   printf("Enter value of a, b, and c");
   scanf("%f%f%f",&a, &b, &c);
   s=(a+b+c)/2;
   ar=sqrt(s*(s-a)*(s-b)*(s-c));
   printf("Area of Triangle = %f", ar);
   getch();
}