Computer Science, asked by vijay00771, 1 year ago

write a java program for sinx series for n= 20

Answers

Answered by QGP
16

Sine Series in Java

The sine function can be written with the help of Taylor Series. The Taylor Series for sine is:


\displaystyle\sin (x)=x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\cdots \\ \\ \\ \implies \Large\boxed{\sin(x) = \sum\limits_{i=0}^{\infty} \frac{(-1)^i}{(2i+1)!}x^{2i+1}}


We need the summation upto 20 terms. So we will set up a loop to run 20 times. We will take user input for the value of x. This value is in radians.


Here's a Java Program:


import java.util.Scanner;       //Importing Scanner


public class Brainly    //Creating class

{

   public static void main(String[] args)      

   {

       Scanner sc = new Scanner(System.in);    //Creating Scanner object

       

       System.out.print("Enter the value of x: ");

       double x = sc.nextDouble();             //User input for x

       

       int n=20;

       

       double Sum = 0;        //Creating and Initialising Sum to 0

       

       for(int i=0;i<n;i++)    //Setting a loop

       {

           Sum += (Math.pow(-1,i)*Math.pow(x,2*i+1))/factorial(2*i+1); //Adding the terms

       }

       

       System.out.println("sin(x) = "+Sum);    //Printing Sum

       

   }

   

   static double factorial(int n) //Function to find factorial

   {

       double fact=1;

       for(int i=2;i<=n;i++)

       {

           fact = fact*i;      //Each integer till n is multiplied

       }

       return fact;        //Returning the factorial

   }

}



Attachments:

CoolestCat015: Wow.. : O
Similar questions