Given n and b, where n is the number of terms in the series and b is the value of the angle in degree.
Program to calculate the sum of cosine series of x and compare the value with the library function’s output.
Formula Used :
cos x = 1 – (x2 / 2 !) + (x4 / 4 !) – (x6 / 6 !) +…
Examples :
Input : n = 3
x = 90
Output : Sum of the cosine series is = -0.23
The value using library function is = -0.000204
Input : n = 4
x = 45
Output :
Sum of the cosine series is = 0.71
The value using library function is = 0.707035
C++
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double cosXSertiesSum(double x,
int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
{
sign = sign * -1;
fact = fact * (2 * i - 1) *
(2 * i);
pow = pow * x * x;
res = res + sign *
pow / fact;
}
return res;
}
int main()
{
float x = 50;
int n = 5;
cout << cosXSertiesSum(x, 5);
return 0;
}
|
Java
import java.lang.Math.*;
class GFG
{
static final double PI = 3.142;
static double cosXSertiesSum(double x,
int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
{
sign = sign * -1;
fact = fact * (2 * i - 1) *
(2 * i);
pow = pow * x * x;
res = res + sign * pow / fact;
}
return res;
}
public static void main(String[] args)
{
float x = 50;
int n = 5;
System.out.println((float)(
cosXSertiesSum(x, 5) * 1000000) /
1000000.00);
}
}
|
Python3
PI = 3.142;
def cosXSertiesSum(x, n):
x = x * (PI / 180.0);
res = 1;
sign = 1;
fact = 1;
pow = 1;
for i in range(1,5):
sign = sign * -1;
fact = fact * (2 * i - 1) * (2 * i);
pow = pow * x * x;
res = res + sign * pow / fact;
return res;
x = 50;
n = 5;
print(round(cosXSertiesSum(x, 5), 6));
|
C#
using System;
class GFG
{
static double PI = 3.142;
static double cosXSertiesSum(double x,
int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
{
sign = sign * -1;
fact = fact * (2 * i - 1) *
(2 * i);
pow = pow * x * x;
res = res + sign * pow / fact;
}
return res;
}
public static void Main()
{
float x = 50;
int n = 5;
Console.Write((float)(cosXSertiesSum(x, n) *
1000000) / 1000000.00);
}
}
|
PHP
<?php
$PI = 3.142;
function cosXSertiesSum($x, $n)
{
global $PI;
$x = $x * ($PI / 180.0);
$res = 1;
$sign = 1; $fact = 1;
$pow = 1;
for ( $i = 1; $i < 5; $i++)
{
$sign = $sign * -1;
$fact = $fact * (2 * $i - 1) *
(2 * $i);
$pow = $pow * $x * $x;
$res = $res + $sign * $pow /
$fact;
}
return $res;
}
$x = 50;
$n = 5;
echo cosXSertiesSum($x, 5);
?>
|
Note that we can also find cos(x) using library function.
C++
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 60.0;
val = PI / 180.0;
ret = cos(x * val);
cout << "The cosine of " << fixed
<< setprecision(6) << x << " is ";
cout << fixed << setprecision(6)
<< ret << " degrees" << endl;
x = 90.0;
val = PI / 180.0;
ret = cos(x * val);
cout << "The cosine of " << fixed
<< setprecision(6) << x << " is ";
cout << fixed << setprecision(6)
<< ret << " degrees" << endl;
return(0);
}
|
C
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 60.0;
val = PI / 180.0;
ret = cos( x * val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);
x = 90.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);
return(0);
}
|
Java
import java.io.*;
class GFG
{
static final double PI = 3.142;
public static void main (String[] args)
{
double x, ret, val;
x = 60.0;
val =(int)PI / 180.0;
ret = Math.cos(x * val);
System.out.print("The cosine of " +
x + " is ");
System.out.print(ret);
System.out.println(" degrees");
x = 90.0;
val = (int)PI / 180.0;
ret = Math.cos( x*val );
System.out.print("The cosine of " +
x + " is ");
System.out.print(ret);
System.out.println(" degrees");
}
}
|
Python3
import math
if __name__=='__main__':
PI = 3.14159265
x = 60.0
val = PI / 180.0
ret = math.cos(x * val)
print("The cosine of is ", x, end=" ")
print(" degrees", ret)
x = 90.0
val = PI / 180.0
ret = math.cos(x * val)
print("The cosine of is ", x, end=" ")
print("degrees", ret)
|
C#
using System;
class GFG
{
static double PI = 3.142;
static public void Main ()
{
double x, ret, val;
x = 60.0;
val = (int)PI / 180.0;
ret = Math.Cos(x * val);
Console.Write("The cosine of " +
x + " is ");
Console.Write(ret);
Console.WriteLine(" degrees");
x = 90.0;
val = (int)PI / 180.0;
ret = Math.Cos(x * val);
Console.Write("The cosine of " +
x + " is ");
Console.Write(ret);
Console.WriteLine(" degrees");
}
}
|
PHP
<?php
$PI =3.14159265;
$x; $ret; $val;
$x = 60.0;
$val = $PI / 180.0;
$ret = cos( $x * $val );
echo "The cosine of is ", $x;
echo "degrees", $ret;
echo "\n";
$x = 90.0;
$val = $PI / 180.0;
$ret = cos( $x * $val );
echo "The cosine of is ", $x;
echo "degrees ", $ret;
?>
|
Output :
The cosine of 60.000000 is 0.500000 degrees
The cosine of 90.000000 is 0.000000 degrees
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.