Wednesday, October 22, 2025

Mathematical Functions in C

 Mathematical Functions in C


C programming provides a rich set of mathematical functions, primarily found within the <math.h> header file. To utilize these functions, this header file must be included at the beginning of the C source code.


Math Functions 


ceil(x) Get smallest integral value that exceeds x. 

floor(x) Get largest integral value less than x. 

fmod(x,y) Divide x by y with integral quotient and return remainder. 

abs(n) Find absolute value of integer n. 

fabs(x) Compute absolute value of x. 

log(x) Compute log(x). 

log10(x) Compute log to the base 10 of x. 

exp(x)  Compute exponential of x 

pow(x, y)    Compute x raised to the power y. 

sqrt( x)    Compute the square root of x. 


 Trigonometric Functions 

sin(x) Compute sine of angle in radians. 

cos(x) Compute cosine of angle in radians. 

tan( x) Compute tangent of angle in radians. 

acos(x) Compute arc cosine of x. 

asin(x)  Compute arc sine of x. 

atan(x) Compute arc tangent of x. 

atan2(y,x) Compute arc tangent of y/x, using the signs of both arguments to determine  the quadrant of the return value. 



Hyperbolic Functions 

sinh(x) Compute the hyperbolic sine of x. 

cosh(x) Compute the hyperbolic cosine of x. 

tanh( x) Compute the hyperbolic tangent of x. 


C Example Program for math functions

#include<stdio.h>

main()

{

    printf("\n           Maths Functions");

    printf("\n           ***************");

    printf("\nCeil(2.546)             = %f",ceil(2.546));

    printf("\nFloor(2.546)            = %f",floor(2.546));

    printf("\nFMod(5.0/2.0)           = %f",fmod(5.0,2.0));

    printf("\nAbs(-5)                 = %f",abs(-5));

    printf("\nFAbs(-5.0)              = %f",fabs(-5.0));

    printf("\nLog(1.234)              = %f",log(1.234));

    printf("\nLog10(1.234)            = %f",log10(1.234));

    printf("\nExp(1.234)              = %f",exp(1.234));

    printf("\nPow(5,3)                = %f",pow(5,3));

    printf("\nSqrt(9)                 = %f",sqrt(9));


    printf("\n         Trigonometric Functions");

    printf("\n         ***********************");

    printf("\nSine(90)                = %f",sin(90*3.14/180));

    printf("\nCosine(90)              = %f",cos(90*3.14/180));

    printf("\nTangent(90)             = %f",tan(45*3.14/180));


    printf("\nArc Sine(0.7)           = %f",asin(0.7));

    printf("\nArc Cosine(0.7)         = %f",acos(0.7));

    printf("\nArc Tangent(0.7)        = %f",atan(0.7));

    printf("\nArc Tangent(0.7/2.0)    = %f",atan2(0.7,2.0));


    printf("\n           Hyperbolic Functions");

    printf("\n           ********************");

    printf("\nHyperbolic Sine(0.7)    = %f",sinh(0.7));

    printf("\nHyperbolic Cosine(0.7)  = %f",cosh(0.7));

    printf("\nHyperbolic Tangent(0.7) = %f",tanh(0.7));

}



Operator Precedence and Associativity in C

Operator Precedence and Associativity in C



Each operator in C has a precedence associated with it. The precedence of operators determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated first.


                  

 Operator   

Description

   Associativity   

Precedence   

( )

Function call

Left-to-Right

1

[ ]

Array element reference

 

+

Unary plus

Right-to-Left

2

-

Unary minus

++

Increment

--

Decrement

!

Logical negation

~

Ones complement

*

Pointer reference (indirection)

&

Address

sizeof

Size of an object

(type)

Type cast(conversion)

 

*

Multiplication

Left-to-Right

3

/

Division

%

Modulus

 

+

Addition

Left-to-Right

4

-

Subtraction

 

<< 

Left Shift

Left-to-Right

5

>> 

Right shift

 

< 

Less than

Left-to-Right

6

<=

Less than or equal to

?

Greater than

>=

greater than or equal to

 

==

Equality

Left-to-Right

7

!=

Inequality

 

&

Bitwise AND

Left-to-Right

8

 

^

Bitwise XOR

Left-to-Right

9

 

|

Bitwise OR

Left-to-Right

10

 

&&

Logical AND

Left-to-Right

11

 

||

Logical OR

Left-to-Right

12

 

?:

Conditional expression

Right-to-Left

13

 

=

Assignments  operators

Right-to-Left

14

*=  /=  %=

+= -=  &=

^=  |=

<<=  ??=\

 

,

Comma operator

Left-to-Right

15

Tuesday, October 21, 2025

Input 2 values. and perform the Athematic Operations (Addition, Subtraction, Multiplication, Division and Reminder Division)

 Input 2 values. and perform the Athematic Operations (Addition, Subtraction, Multiplication, Division and Reminder Division)


#include<stdio.h>

main()

{

      int a,b,c,d,e,f,g;

      c=d=e=f=g=0;

      scanf("%d%d",&a,&b);

      c=a+b;

      d=a-b;

      e=a*b;

      f=a/b;

      g=a%b;

      printf("\nC=%d\nD=%d\nE=%d",c,d,e);

      printf("\nF=%d\nG=%d",f,g);

}

Output






Formatted Input. scan function in C

 Formatted Input. scan function in C



Formatted Input: Formatted input refers to an input data that has been arranged in a particular format.

scanf(): Scanf() function can be used input  the data into the memory from the standard input device.  This function can be used to enter any combination of numerical Values, single characters and strings.  The function returns number of data items.

Syntax:- 

      scanf (“conversion strings”, &arg1,&arg2,--------&arg-n);

Where conversion string reefers to a string containing certain required formatting information and arg1, arg2----arg-n are arguments that represent the individual input data items.

Example:                   

Where %s , %d, %f, %c with % are conversion characters.  The conversion characters indicate the type of the to corresponding data. Commonly used conversion characters for data input.

 int a;  float b; char n,sn[20];

        scanf(“%d”,&a);

        scanf(“%f”,&b;

        scanf(“%c”,&n);

        scanf(“%s”,sn);

        scanf(“%d%f%c%s”,&a,&b,&n,sn);

Storage Class in C

 Storage Class in C


            

The scope and lifetime of a variable or / and function within a C program is defined by storage class. There are four storage classes in C


auto -      It is the default storage class for all variables and / or   functions.


register - Allows for defining variables to store in CPU register  instead of RAM. Unary operator is not applied for register    variable.


static –   The static storage class allows the updated variable values  available  for the next time when the function, in which the  variable is defined,  is invoked for the next time.


extern -   It allows a global variable to be visible to all the program  files /  external files ( C programs).

     

Calculate Simple Interest

 Calculate Simple Interest with following detail. p=1000, n=5 and  r=2.5


#include<stdio.h>

main()

{

      int n=5;

      float p=1000,r=2.5,si=0.0;

      si=p*n*r/100;

      printf("\nSimple Interest %.2f",si);

      Output