Sample Program 5: Use of Math Function in C

4 minute read
0
We often use standard mathematical functions in our programs, such as sin, cos, exp, etc. We shall see now the use of a mathematical function in our program.



Let's look at the program to understand what's happening here,

    /* ...... PROGRAM USING COSINE FUNCTION .......*/
    #include<stdio.h>
    #include<math.h>
    #define PI 3.14
    #define MAX 180

    main()
    {
        int agl= 0;
        float x, y;
        printf("Angle \t cos(agl)\n\n");
        while(agl <= MAX)
        {
            x= (PI/MAX) * agl;
            y= cos(x);
            printf("%15d %12.4f\n", agl, y);
            agl+= 10; // it means, agl= agl + 10
        }
    }

The output of this program is

    Angle cos(agl)
    
    
     0 1.0000 10 0.9848 20 0.9398 30 0.8662 40 0.7663 50 0.6431 60 0.5005 70 0.3426 80 0.1743 90 0.0008 100 -0.1728 110 -0.3411 120 -0.4991 130 -0.6419 140 -0.7652 150 -0.8654 160 -0.9392 170 -0.9845 180 -1.0000

The standard mathematical functions are defined and kept as a part of the C math library. If we want to use any of these mathematical functions, we must add on #include instruction in the program. The instruction is of the form:

            #include<math.h>

It is a compiler directive that instructs the compiler to link the specified mathematical functions from the library. math.h is the filename containing the required function. 
Another #include instruction that is often required and we must always use it. That is:

            #include<stdio.h>

stdio.h refers to the standard I/O(input and output) header file containing standard input and output functions.

The #include Directive :

C programs are divided into modules and functions. Some functions are written by users, like us, and many others are stored in the C library. Library functions are grouped category-wise and stored in different files called header files. If we want to access the functions in the library, we must tell the compiler by using the preprocessor directive #include. Something like this:

            #include<filename>

The filename is the name of the library file that contains the required function definition.
*Remember, Preprocessor directives are placed at the beginning of a program. 


QUESTION

Q. write a c program to input two numbers: a and b. next, compute the value of raised to the power of b.

Ans: Certainly! Here's a simple C program that takes two numbers 'a' and 'b' as input and computes the value of 'a' raised to the power of 'b': 

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

    int main() {
        double a, b, result;

        // Input the values of 'a' and 'b'
        printf("Enter the value of 'a': ");
        scanf("%lf", &a);
   
        printf("Enter the value of 'b': ");
        scanf("%lf", &b);

        // Calculate the result
        result = pow(a, b);

        // Display the result
        printf("%.2lf raised to the power %.2lf is %.2lf\n", a, b, result);

        return 0;
    }

The output of this program is:

Enter the value of 'a': 2 Enter the value of 'b': 2 2.00 raised to the power 2.00 is 4.00

In this program, we use the pow() function from the math.h library to calculate the power of 'a' raised to 'b'. Make sure to compile and run this program in a C environment to use it.



----------------------------------------------------------------------------------------------------------------------------


Explore our website for valuable C programming resources and enhance your coding skills today!.


Learn C programming language from basic to advanced here (Click here or on the image)


Stay Strong, keep learning, and keep growing.






Post a Comment

0Comments
Post a Comment (0)