Mathematical Functions in c programming langauage

C Programming Language Function in C Language (Article) Function in C Language (Program)

1076

This program demonstrates the usage of various mathematical functions provided in the "math.h" header file of C. It initializes a variable "a" with the value 0.25 and then uses various functions such as abs(), acos(), asin(), atan(), atan2(), cos(), sin(), tan(), and sqrt() to perform mathematical operations on "a" and store the result in variable "b". Finally, it prints the value of "b" using the printf() function.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
    float a;
    float b;
    float pi = 3.14;
    a = 0.25;

    // Printing the value of a
    printf("value a = %.2f \n", a);

    // Calculating and printing the absolute value of a
    b = abs(-a);  // Note: abs() is for integers, fabs() should be used for floats
    printf("abs(a)=%.2f \n", b);

    // Calculating and printing the arc cosine of a
    b = acos(a);
    printf("acos(a)=%.2f \n", b);

    // Calculating and printing the arc sine of a
    b = asin(a);
    printf("asin(a)=%.2f \n", b);

    // Calculating and printing the arc tangent of a
    b = atan(a);
    printf("atan(a)=%.2f \n", b);

    // Calculating and printing the arc tangent of a divided by 5
    b = atan2(a, 5);
    printf("atan(a,5)=%.2f \n", b);

    // Calculating and printing the cosine of a
    b = cos(a);
    printf("cos(a)=%.2f \n", b);

    // Calculating and printing the sine of a
    b = sin(a);
    printf("sin(a)=%.2f \n", b);

    // Calculating and printing the tangent of a
    b = tan(a);
    printf("tan(a)=%.2f \n", b);

    // Calculating and printing the square root of a
    b = sqrt(a);
    printf("sqrt(a)=%.2f \n", b);

    return 0;
}

Output:

value a = 0.25
abs(a)=0.00
acos(a)=1.32
asin(a)=0.25
abs(a)=0.24
atan(a,5)=0.05
cos(a)=0.97
sin(a)=0.25
tan(a)=0.26
sqrt(a)=0.50
Press any key to continue . . .

Explanation:

  • Include Libraries: The #include directives at the beginning include the standard input-output library (stdio.h), the standard library (stdlib.h), and the math library (math.h).

  • Variable Declarations:

    • float a and float b are declared to store floating-point numbers.
    • float pi is declared and initialized to 3.14, though it is not used in this program.
  • Initialization and Printing:

    • a is initialized to 0.25.
    • The value of a is printed with 2 decimal places using printf.
  • Mathematical Operations and Printing:

    • abs(-a) calculates the absolute value of -a. Note: abs() is used for integers, so fabs() should be used for floating-point numbers to avoid incorrect results.
    • acos(a) calculates the arc cosine (inverse cosine) of a.
    • asin(a) calculates the arc sine (inverse sine) of a.
    • atan(a) calculates the arc tangent (inverse tangent) of a.
    • atan2(a, 5) calculates the arc tangent of a divided by 5, considering the sign of both arguments.
    • cos(a) calculates the cosine of a.
    • sin(a) calculates the sine of a.
    • tan(a) calculates the tangent of a.
    • sqrt(a) calculates the square root of a.
  • Printing Results: Each result is printed with 2 decimal places using printf.

  • Return Statement: The program ends with a return 0;, indicating successful execution.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.