Java Program TO FInd The Value Of Sinx, Cosx and E^x by using taylor series
Without Using Any String Functions
Sinx=x-(x^3/3!)+(x^5/5!)-.....
Cos x=1-(x^2/2!)+(x^4/4!)-.....
E^x=1+(x^1/1!)+(x^2/2!)+(x^3/3!)+......
import java.io.*;
class TaylorS
{
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch=0,fact,n;
double x,term,sum,i,var;
while(ch!=4)
{
System.out.print("\n\n\n\t----------------");
System.out.print("\n\t******MENU******");
System.out.print("\n\t----------------");
System.out.print("\n\n\t[1].sinx\n\n\t[2].cosx\n\n\t[3].e^x\n\n\t[4].Exit.");
System.out.print("\n\n\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1 : System.out.print("\n\n\t======Sine Series======");
System.out.print("\n\t-----------------------");
System.out.print("\n\n\tEnter the value of x : ");
x=Double.parseDouble(br.readLine());
System.out.print("\n\n\tEnter the number of terms : ");
n=Integer.parseInt(br.readLine());
var=x;
x=((x*3.14)/180);
sum=x;term=x;
for(i=1;i<n;i++)
{
term*=((-1)*x*x)/(2*i*(2*i-1));
sum+=term;
}
System.out.print("\n\n\tsin"+var+" = "+sum);
break;
case 2 : System.out.print("\n\n\t======Cosine Series======");
System.out.print("\n\t-------------------------");
System.out.print("\n\n\tEnter the value of x : ");
x=Double.parseDouble(br.readLine());
System.out.print("\n\n\tEnter the number of terms : ");
n=Integer.parseInt(br.readLine());
var=x;
x=((x*3.14)/180);
sum=term=1;
for(i=1;i<n;i++)
{
term*=((-1)*x*x)/(2*i*(2*i-1));
sum+=term;
}
System.out.print("\n\n\tsin"+var+" = "+sum);
break;
case 3 :
System.out.print("\n\n\t======Exponential Series======");
System.out.print("\n\t------------------------------");
System.out.print("\n\n\tEnter the value of x : ");
x=Double.parseDouble(br.readLine());
System.out.print("\n\n\tEnter the number of terms : ");
n=Integer.parseInt(br.readLine());
sum=fact=1;var=x;
for(i=1;i<n;i++)
{
fact*=i;
term=Math.pow(x,i)/fact;
sum+=term;
}
System.out.print("\n\n\tsin"+var+" = "+sum);
break;
case 4 : System.out.print("\n\n\tGOOD BYE...");
break;
default : System.out.print("\n\n\tINVALID CHOICE !!\n\n\tTRY AGAIN.");
break;
}
}
}
}
Loading...
|
Comments :
Post a Comment