# Program to calculate the value of sin(x) and cos(x) using Expansion

• Difficulty Level : Easy
• Last Updated : 14 Dec, 2021

Given a value of angle, you need to calculate Sin and Cos values corresponding to it.

For sin function

Examples:

Input : 90
Output : 1

## C++

 // CPP code for implementing sin function#include #include using namespace std; // Function for calculating sin valuevoid cal_sin(float n){       float accuracy = 0.0001, denominator, sinx, sinval;         // Converting degrees to radian    n = n * (3.142 / 180.0);     float x1 = n;         // maps the sum along the series    sinx = n;                 // holds the actual value of sin(n)    sinval = sin(n);       int i = 1;    do    {        denominator = 2 * i * (2 * i + 1);        x1 = -x1 * n * n / denominator;        sinx = sinx + x1;        i = i + 1;    } while (accuracy <= fabs(sinval - sinx));    cout << sinx;} // Main functionint main(){    float n = 90;    cal_sin(n);    return 0;}

## Java

 import static java.lang.Math.sin; // JAVA code for implementing sin function class GFG { // Function for calculating sin valuestatic void cal_sin(float n){        float accuracy = (float) 0.0001, denominator, sinx, sinval;         // Converting degrees to radian    n = n * (float)(3.142 / 180.0);     float x1 = n;         // maps the sum along the series    sinx = n;                 // holds the actual value of sin(n)    sinval = (float)sin(n);        int i = 1;    do    {        denominator = 2 * i * (2 * i + 1);        x1 = -x1 * n * n / denominator;        sinx = sinx + x1;        i = i + 1;    } while (accuracy <= sinval - sinx);       System.out.println(sinx);} // Main function      public static void main(String[] args) {        float n = 90;    cal_sin(n);         }}

## Python3

 # Python3 code for implementing# sin functionimport math; # Function for calculating sin valuedef cal_sin(n):     accuracy = 0.0001;         # Converting degrees to radian    n = n * (3.142 / 180.0);         x1 = n;         # maps the sum along the series    sinx = n;             # holds the actual value of sin(n)    sinval = math.sin(n);    i = 1;    while(True):             denominator = 2 * i * (2 * i + 1);        x1 = -x1 * n * n / denominator;        sinx = sinx + x1;        i = i + 1;        if(accuracy <= abs(sinval - sinx)):            break;             print(round(sinx)); # Driver Coden = 90;cal_sin(n);     # This code is contributed by mits

## C#

 // C# code for implementing sin functionusing System; class GFG{// Function for calculating sin valuestatic void cal_sin(float n){    float accuracy = (float) 0.0001,                      denominator, sinx, sinval;         // Converting degrees to radian    n = n * (float)(3.142 / 180.0);     float x1 = n;         // maps the sum along the series    sinx = n;             // holds the actual value of sin(n)    sinval = (float)Math.Sin(n);        int i = 1;    do    {        denominator = 2 * i * (2 * i + 1);        x1 = -x1 * n * n / denominator;        sinx = sinx + x1;        i = i + 1;    } while (accuracy <= sinval - sinx);         Console.WriteLine(sinx);} // Driver Codestatic public void Main (){    float n = 90;    cal_sin(n);}} // This code is contributed by jit_t

## PHP

 

## Javascript

 

Output:

1

For cos function

Examples:

Input : 30
Output : 0.86602

## C++

 // CPP code for implementing cos function#include #include using namespace std; // Function for calculationvoid cal_cos(float n){    float accuracy = 0.0001, x1, denominator, cosx, cosval;         // Converting degrees to radian    n = n * (3.142 / 180.0);         x1 = 1;         // maps the sum along the series    cosx = x1;                 // holds the actual value of sin(n)    cosval = cos(n);    int i = 1;    do    {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;    } while (accuracy <= fabs(cosval - cosx));    cout << cosx;} // Main functionint main(){    float n = 30;    cal_cos(n);}

## Java

 // Java code for implementing cos function import static java.lang.Math.cos; class GFG {// Function for calculation static void cal_cos(float n) {    float accuracy = (float) 0.0001, x1, denominator, cosx, cosval;    // Converting degrees to radian    n = n * (float) (3.142 / 180.0);    x1 = 1;    // maps the sum along the series    cosx = x1;    // holds the actual value of sin(n)    cosval = (float) cos(n);    int i = 1;    do {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;             }    while (accuracy <= cosval - cosx);    System.out.println(cosx);     } // Main functionpublic static void main(String[] args) {    float n = 30;    cal_cos(n);     }}

## Python3

 # Python 3 code for implementing cos function from math import fabs, cos # Function for calculationdef cal_cos(n):    accuracy = 0.0001     # Converting degrees to radian    n = n * (3.142 / 180.0)         x1 = 1         # maps the sum along the series    cosx = x1         # holds the actual value of sin(n)    cosval = cos(n)    i = 1     denominator = 2 * i * (2 * i - 1)    x1 = -x1 * n * n / denominator    cosx = cosx + x1    i = i + 1    while (accuracy <= fabs(cosval - cosx)):        denominator = 2 * i * (2 * i - 1)        x1 = -x1 * n * n / denominator        cosx = cosx + x1        i = i + 1     print('{0:.6}'.format(cosx)) # Driver Codeif __name__ == '__main__':    n = 30    cal_cos(n) # This code is contributed by# Sahil_Shelangia

## C#

 // C# code for implementing cos function using System;class GFG {// Function for calculation static void cal_cos(float n) {    float accuracy = (float) 0.0001, x1, denominator, cosx, cosval;    // Converting degrees to radian    n = n * (float) (3.142 / 180.0);    x1 = 1;    // maps the sum along the series    cosx = x1;    // holds the actual value of sin(n)    cosval = (float) Math.Cos(n);    int i = 1;    do {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;             }    while (accuracy <= cosval - cosx);    Console.WriteLine(cosx);     } // Main functionstatic void Main() {    float n = 30;    cal_cos(n);     }}// This code is contributed by mits

## PHP

 

## Javascript

 

Output:

0.86602

This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.