Q:

C Program to find Cosine Series using function cos(x)

0

Write a C Program to find Cosine Series using function cos(x). Here’s simple C Program to find Cosine Series using function cos(x) in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Numbers in C

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C Data Types.

Here is source code of the C Program to find Cosine Series using function cos(x). The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

SOURCE CODE : :

/*  C Program to find Cosine Series using function cos(x)  */

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

int factorial(int n)
{
        int i,fact=1;
        for(i=1;i<=n;i++)
                fact=fact*i;
        return fact;
}


int main()
{
        double x, cosx=1, z, term;
    int j, n, i,sign=1;

        printf("\nEnter the value for x : ") ;  //Example 45 180 90 360
        scanf("%lf", &x) ;
        printf("\nEnter the value for n : ") ;   //Example 10 20
        scanf("%d", &n) ;

        x = x * 3.14159 / 180 ;   //converting degree to radian
    i=0;
    z=cos(x);

        for(i=2;i<=n;i=i+2)
    {
        sign=-sign;
                term=sign*pow(x,i)/factorial(i);
        cosx=cosx+term;
    }

        printf("\ncos(x) is approximately %.15lf\n", cosx);
    printf("\ncos(x) by default function is %.15lf\n", z);

    return 0;
}

OUTPUT : :

/*  C Program to find Cosine Series using function cos(x)  */

Enter the value for x : 120

Enter the value for n : 20

cos(x) is approximately -0.500539546249695

cos(x) by default function is -0.499998467948436

Process returned 0

Above is the source code for C Program to find Cosine Seriesusing function cos(x) which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Number Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to find Sine Series using Functions Sin(... >>
<< C program to find Sum of series S=1+(1+2)+(1+2+3)+...